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

198
Visualizações
execution order finally() in promises

I was expecting the finally to run after resolving all promises, but I see that it runs after resolving the first promise. So I have two questions: (1) Is the finally one per promise just like the catch, that is, can I have a finally per promise or because this behavior? (2) If I need a finally to run after resolving all promises, do I need only one at the end, just like catch?

let variable = new Promise((resolve, reject) => {
            
            console.log('empiezo 1era promesa, espero 9');
            setTimeout(() => {
                resolve(1);
            }, 9000);
    })
    .finally(() => {

        console.log('empieza finally');
    })
    .then(data => {
        
        return new Promise((resolve, reject) => {
            
            console.log('1era promesa resulta, empiezo 2da promesa, espero 5 seg');
            setTimeout(() => {
                resolve(2);
            }, 5000);
        });
        
    })
    .then(data => {
        console.log('2da promesa resulta, ya termino todo');
        console.log(data);
    })

Ouput: empiezo 1era promesa, espero 9 empieza finally 1era promesa resulta, empiezo 2da promesa, espero 5 seg 2da promesa resulta, ya termino todo 2

My doubt is because I always see examples with only one finally at the end and this one is executed after resolving all the promises.

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

0

All of the then, catch and finally methods are "per promise", the promise they were called on. They all return a new promise that will resolve after the callback(s) have run, and they do not know about any other promises further down the chain (or even in other chains).

If you want your finally callback to run after all the promises in your chain have settled, you should also chain it in the end.

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