Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda