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

105
Views
Promise doesn't seem to be resolved with await

I have found something interesting while playing with Promises and awaits:

function returnPromise() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('result of promise');
    }, 1000);
  });
}


async function sth() {
  let result = await returnPromise();
  console.log('result: ', result);
  console.log('flag 1');
}

sth();

In this first example, the result of the promise prints out correctly. However, consider the second example:

function returnPromise() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('result of promise');
    }, 1000);
  });
}

async function sth2() {
  let result = returnPromise();
  await result;
  console.log('result: ', result);
  console.log('flag 1');
}

sth2();

Here, the result is {} (which I think means the Promise is still pending). This doesn't make sense to me because I thought the line 'await result' was supposed to block till the Promise resolved.

My question is:

  1. Is this expected behavior? If it is, what is the logic behind it?
  2. If this isn't what's expected, why is this happening?

Thank you.

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

0

I thought the line 'await result' was supposed to block till the Promise resolved.

Well, it blocks further execution of that async function - but yes.

But the result is still a Promise.

await will take the thenable on its right side, wait for it to finish, and extract the resolve value from it. But the thenable will remain a thenable - it doesn't change the Promise itself (it doesn't change the Promise expression into the value it resolves to).

await someThenable

extracts the resolve value from someThenable, but someThenable remains a thenable - logging someThenable, whether before or after the await, will still log the thenable, and not the resolve value.

about 4 years ago · Juan Pablo Isaza Report

0

let result = returnPromise();

This line sets the value of result to be a reference to the Promise object. It does not automagically change to be the result of the awaited promise whenever you call await on it later on.

You can tell by the fact that there's still a waiting period that the await keyword is working. However, you're throwing away the result value.

await will give you and rvalue that you can assign to a variable:

let actualResult = await result;
// or
result = await result; // but you shouldn't do this
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!