DEV Community

Discussion on: Async-Await vs. Then to Avoid Callback Hell 📞😈

Collapse
 
smolinari profile image
Scott Molinari

Correct me if I am wrong, but the console log with async/ await is only blocked, because the data variable is in it. If you had of done the same above with the .then example, that console log would be blocked too.

Or put another way, take out the data variable and the async/ await example won't block either.

Scott

Collapse
 
sergey_solovev_ca17ee912a profile image
Leo Fitz

That's not true. Await will wait for the promise to be resolved or rejected before moving to the next line. Consider the following example:

function PromiseToWait() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("Promise 1 resolved");
    }, 5000);
  });
}

console.log("Await promesses:");
const start = Date.now();
const response = await PromiseToWait();
console.log(response);
console.log("Am I blocking?");
const end = Date.now();
console.log("Time taken: ", end - start);
Enter fullscreen mode Exit fullscreen mode

It will produce:

Image description

Whether you use response or not, it will go line by line.

I trust that this explanation has been helpful. If you have any further questions, please don't hesitate to ask.

Best regards!