Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Await doesn't wait

Tags:

javascript

function resolveAfter2Seconds(x) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(x);
        }, 2000);
    });
}

async function f1() {
    var x = await resolveAfter2Seconds(10);
    console.log(x); 
}

f1();
let x =  3;

Why do we see the following scenario?

  1. Entering the f1. Stops on await.
  2. Returns from f1 (console.log(x) command doesn't execute)
  3. assign 3 to x (ERROR! await skipped, js steps forward)
  4. Return to f1 on line "console.log(x)". Print x.

Why JS not waiting on await and steps forward? May you give me an advice?

like image 531
Daniil Vorobeyv Avatar asked May 02 '19 20:05

Daniil Vorobeyv


People also ask

How do you use await correctly?

async and awaitInside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

Does await wait for then?

The await keyword before a promise makes JavaScript wait until that promise settles, and then: If it's an error, an exception is generated — same as if throw error were called at that very place. Otherwise, it returns the result.

What is the use of await?

The await operator is used to wait for a Promise and get its fulfillment value. It can only be used inside an async function or a JavaScript module.

Which is better promise or await?

Using Async/Await makes it easier to read and understand the flow of the program as compared to promise chains.


2 Answers

f1 is asynchronous (the await only occurs within that asynchronous context). Therefore, f1() is executed, and because it's async, the let x = 3; line executes immediately without waiting.

If you also await the call to f1(), it should do what you're expecting. However in order to use await, you must wrap that code inside another async function, and then execute that function.

Demo (no await):

function resolveAfter2Seconds(x) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(x);
        }, 2000);
    });
}

async function f1() {
    var x = await resolveAfter2Seconds(10);
    console.log(x); 
}

f1();
let x =  3;
console.log(x);

Working version (with extra await):

function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
}

async function f1() {
  var x = await resolveAfter2Seconds(10);
  console.log(x);
}

async function f2() {
  await f1();
  let x = 3;
  console.log(x);
};

f2();
like image 135
ADyson Avatar answered Oct 19 '22 00:10

ADyson


More simpler

(async function() {

  function resolveAfter2Seconds(x) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(x);
      }, 2000);
    });
  }

  async function f1() {
    var x = await resolveAfter2Seconds(10);
    console.log(x);
  }


  await f1();
  let x = 3; 
  console.log(x);
})();
like image 41
Ank Avatar answered Oct 18 '22 22:10

Ank