Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
Javascript Promise within Class

Fun with promises today ... I'm trying to figure out why I can't refer to this.promise to resolve it with another method, it's always null.

Any enlightenment is appreciated :)

export default class Example {
  constructor() {
    this.promise = null
  }

  test() {
    this.promise = new Promise((resolve, reject) => {
      this.something()
    }).then(resolve => {
      // resolved, do something else
    }).catch(reject => {
      console.log('reject', reject)
    })
  }

  something() {
    this.promise.resolve()
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're trying to use this.promise in something before it's value changes from null. Another way to do this is to pass the resolve as an argument:

class Example {
  constructor() {
    this.promise = null;
  }
  test() {
    this.promise = new Promise((resolve, reject) => {
      this.something(resolve);
    }).then(result => {
      console.log('resolve', result);
    }).catch(error => {
      console.log('reject', error);
    });
  }
  something(resolve) {
    resolve(1);
  }
}
const example = new Example();
example.test();

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda