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

181
Visualizações
Reusing an existing Promise object if exists

I have a Dialog object that will show, well, dialogs. There are many entry points to show dialogs, e.g. yesNo(), message(), confirm(), etc. However, all these methods basically call the same other method, called showSimpleDialog(title, message, buttons).

I'd like all these methods (showSimpleDialog too) to return a promise, but there's a snag:

yesNo() {
  return new Promise((resolve, reject) => {
    axios
      .get(......)
      .then(this.showSimpleDialog(...));
  }
}

As you can see, I am prevented in the above example from either returning the promise that showSimpleDialog would make or by passing the instanced Promise to showSimpleDialog.

The former is impossible because we're already in a different Promise by the time we have access to it. The latter because the Promise object itself is not yet available within the constructor. Well, technically, in this particular case it is (exactly because we're already in a different Promise), but some of my entry functions are synchronous, some asynchronous and I simply can't use the same code pattern to achieve the same effect in both cases.

I investigated the thing and I found this, but the author suggests the approach is flawed and archaic to begin with.

So, what would be the correct approach to return a functioning Promise from ALL entry points while the entry points would still be free to reusing each other's Promises?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If I understand correctly, this.showSimpleDialog(...) also returns a Promise, right?

If you want yesNo() to return the Promise retunred by this.showSimpleDialog(...)

yesNo() { 
    return axios
        .get(......)
        .then(()=>{ 
            return this.showSimpleDialog(...);
        });
}

That being said, consider using async/await, especially when dealing with multiple sequential promises, if possible.

about 4 years ago · Juan Pablo Isaza Relatório

0

Your code is calling this.showSimpleDialog immediately (synchronously) without waiting for any promise to resolve (the axios one). This is because the code doesn't pass a function to the then method, but instead executes this.showSimpleDialog. This execution returns a promise (presumably), but then expects a function as argument, not a promise.

So you need to make sure to pass a callback to then, and let that callback return a promise. This way promises will be chained:

    .then(() => this.showSimpleDialog(...));

It is also important to make that callback an arrow function, since you'll be referencing this, which is intended to be the this on which yesNo is called.

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