Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
Why do these two await lines execute in parallel?

In below code, both c and c2 variables are awaiting for different promise instances and should run in serial but when run, the program completes in 100 miliseconds instead of 200.

function a1() {
  return new Promise((x, y) => {
    setTimeout(function () {
      x(5);
    }, 100);
  });
}

async function run() {
  let t1 = Date.now();
  let b = a1();
  let b2 = a1();

  let c = await b;
  let c2 = await b2;
  console.log(Date.now() - t1);
}

run().then(function () {
  console.log('@');
});

Why is this behavior legal if await is defined as a serial operation?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are calling a1() for both b and b2 when declaring them. Thus, by the time you await, the timeouts have already run and they each hold a promise, the await just resolves those promises.

If you want to see the full delay you should asign a1 to your b variables and then await the calls when declaring c, or simply await a1() twice.

  let b = a1;
  let b2 = a1;

  let c = await b();
  let c2 = await b2();

function a1() {
  return new Promise((x, y) => {
    setTimeout(function () {
      x(5);
    }, 100);
  });
}

async function run() {
  let t1 = Date.now();

  let b = await a1();
  let b2 = await a1();
  
  console.log(Date.now() - t1);
}

run().then(function () {
  console.log('@');
});

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!