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

220
Visualizações
Is it possible for later evaluation win in Promise.race() and Promise.all() even if they consume the same amount of time?

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all, two evaluations in the following code runs asynchronously.

var p2 = Promise.all([1337, "hi"]); // non-promise values will be ignored, but the evaluation will be done asynchronously

and a question occurs to me that, in the following example:

async function sleep(x) {
    await new Promise((resolve) => setTimeout(resolve, x))
}
async function test(x) {
    await sleep(1000);
    return x
}

async function run(){
    let promises = []
    promises.push(test(1))
    promises.push(test(2))
    let res = await Promise.race(promises)
    console.log(res)
}
run()
// I ran it for several times, and it always give me 1

Since these two evaluation ran asynchronously and they consume the same amount of time, is it possible for this program to give me 2 in some cases?

What I dont understand is:

  • Why this program always gives me 1? If multiple evaluations consume the same amount of time, does javascript promise the earlier one always finish earlier?
  • Does thread context switching happens in Promise.all()? I've tried another example code of multithreading, and it always gives me 100.
let a = 0
async function test1() {
    a++;
}
async function run(){
    let promises = []
    for (let i=0;i<100;i++){
        promises.push(test1())
    }
    await Promise.all(promises)
    console.log(a)
}
run()
// It always give me 100

It seems to never violate atomicity, why?

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

0

No. How Event loop in javascript works is, it will put two timeout callbacks in a "timeout queue" and because the both timeouts have the same timeout delay (1000) their callbacks will be executed in the order of their appearance in the "timeout queue". First "1", then "2".

about 4 years ago · Juan Pablo Isaza Relatório

0

Why this program always gives me 1? If multiple evaluations consume the same amount of time, does javascript promise the earlier one always finish earlier?

The other answer answered this – the timeouts get resolved in the order they're put into the queue. The timeout deadline for the 2 promise is always necessarily later than the one for 1 promise anyway, since some time will have passed between promises.push(test(1)) and promises.push(test(2)).

The second question:

Does thread context switching happens in Promise.all()? I've tried another example code of multithreading, and it always gives me 100.

No, you didn't try multithreading because there is no multithreading in regular JavaScript.

An async function is synchronous until the first await in it (and sugars its return value to be wrapped in a promise), so

async function test1() {
  a++;
  // implicit return undefined;
}

is equivalent to

function test1() {
  a++;
  return Promise.resolve(undefined);
}

This means that

async function run() {
  let promises = []
  for (let i = 0; i < 100; i++) {
    promises.push(test1())
  }
  await Promise.all(promises)
  console.log(a)
}

is equivalent to

async function run() {
  let promises = []
  for (let i = 0; i < 100; i++) {
    a++;
    promises.push(Promise.resolve(undefined));
  }
  await Promise.all(promises);
  console.log(a);
}

which is plainly synchronous in how it modifies a.

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