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

189
Vistas
Do something when Promise outside then finished

Here is the code.

function getPromise():Promise<any> {
    let p = new Promise<any>((resolve, reject) => {
        //some logical
        resolve(data);
    });
    p.finally(()=>{ 
        //I want do something when outside then finished! 
        console.log("finally hit");
    });
    return p;
}

function doPromise():void {
    a.getPromise().then(data => { console.log("then hit"); });
}

But the finally runs before then. So how can I do something after outside then.

I do not want to add finally after then, because the promise called many places. so I want do it in one place.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Maybe you can incorporate callback into getPromise?


function getPromise(callback):Promise<any> {
    let p = new Promise<any>((resolve, reject) => {
        //some logical
        resolve(data);
    });
    p.then(callback)
    p.finally(()=>{ 
        //I want do something when outside then finished! 
        console.log("finally hit");
    });
    return p;
}

function doPromise():void {
    getPromise(data => { console.log("then hit"); });
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

What you have to do is retrun a callback which you can use later on like this:

type GetPromise = {
  p: Promise<any>;
  finalCallback: () => void;
}
function getPromise():GetPromise {
  let p = new Promise<any>((resolve, reject) => {
      //some logical
      resolve(data);
  });
  finalCallback(()=>{ 
      //I want do something when outside then finished! 
      console.log("finally hit");
  });
  return {p, finalCallback};
}

function doPromise():void {
  const {p: promise, finalCallback} = getPromise()
  promise().then(data => { 
    console.log("then hit");  
    finalCallback();
  });
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your getPromise() its a Promise itself, so your .then() will run only after it concludes, in this case, after the .finally().

You can put the .then() inside of getPromise() or move the .finally() to the outside after the .then().

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