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

194
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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