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

217
Visualizações
Does Promise.race ignore promises after the first promise resolves

code snippet

I have this code snippet, the timeoutPromise is supposed to resolve after 250ms IF the responsePromise does not resolve in that amount of time. Right now I see the timeoutPromise resolves regardless of the responsePromise.

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

0

All Promise.race() does it report to the caller which of the promises you passed it resolved/rejected first. It does not affect whether they resolve/reject and does not stop the other promises from resolving after the first one has resolved. The operations in those other promises still keep going and they have whatever outcome they would have, regardless of using Promise.race().

Look at this example which you can run right in the snippet:

function resolveMs(ms) {
    return new Promise(resolve => {
        setTimeout(() => {
            console.log(`resolveMs(${ms}) resolving`);
            resolve(ms);
        }, ms);
    });
}

console.log('Starting...');
Promise.race([resolveMs(100), resolveMs(50), resolveMs(200)]).then(result => {
    console.log(`Final result: ${result}`);
});

That generates this output:

resolveMs(50) resolving
Final result: 50
resolveMs(100) resolving
resolveMs(200) resolving

There you can clearly see that all three promises resolve on their expected schedule. Promise.race() itself resolves when the first of the three promises you passed it resolve, but that does not affect the other two operations. They keep doing what they were doing.

You can think of it like an 800m running race. Promise.race() reports the winner of the race (that is its job), but just because there's a winner, the other participants don't stop running. They still finish the race too in whatever time they would have if they ran on their own.

If you want the other operations to somehow stop when the first resolves, you'd have to manually code things so that you cancelled the other two operations and they would have to represent operations that are cancellable as there is no automatic, built-in mechanism for stopping asynchronous operations that are already underway. In my example above, one would have to call clearTimeout() on the remaining timers (and you would have to save the timerID values somewhere so you could call clearTimeout() on them).

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