Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event loop flow with Async/Await

I have been experimenting with Async/Await commands in Javascript and noticed something I'm confused about.

When I run the following code in Visual Studio Code in debug mode, after I get to the first 'await' line of code it jumps to the end of the function and then continues sequentially through the rest of the code including other 'await' lines. Why is this?

const axios = require("axios");
const apiURL = 'https://jsonplaceholder.typicode.com/posts/1'

async function multipleRequestsAsync() {
  try {
    console.log("starting...")
    const response1 = await axios.get(apiURL);
    console.log(response1.data);
    const response2 = await axios.get(apiURL);
    console.log(response2.data);
    const response3 = await axios.get(apiURL);
    console.log(response3.data);
  } catch (error) {
    console.error(error);
  }
}

multipleRequestsAsync();

Here is the code in jsfiddle.

Here is an animated gif of what I'm talking about. enter image description here

like image 730
Arthur Frankel Avatar asked Mar 22 '18 02:03

Arthur Frankel


People also ask

How does async await work in event loop?

An async function can contain an await expression, that pauses the execution of the function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value.

Does using async await block event loop?

Though it creates a confusion, in reality async and await will not block the JavaScript main thread. Like mentioned above they are just syntactic sugars for promise chaining. Putting other way both code snippets below are same.

Why we use async and await in flutter?

We put await in front of an asynchronous function to make the subsequence lines waiting for that future's result. We put async before the function body to mark that the function support await . An async function will automatically wrap the return value in Future if it doesn't already.

Why is that using async functions with event handler is problematic?

Using async functions with event handlers is problematic, because it can lead to an unhandled rejection in case of a thrown exception: const ee = new EventEmitter(); ee.


1 Answers

It probably signifies the point at which the function returns the promise. If you include some other code after the call to your async function, that would be stepped through before the execution continues after the await in a future turn of the event loop. Consider

async function multipleRequestsAsync() {
  try {
    console.log("starting...");                // 3
    const response1 = await axios.get(apiURL); // 4
    console.log("done");                       // 7
  } catch (error) {
    console.error(error);
  }
}                                              // 5

console.log("before start");                   // 1
const promise = multipleRequestsAsync();       // 2
console.log("continuing...");                  // 6

You'd get the output

before start
starting...
continuing
// later:
done

The breakpoint at the end of the function (5) would occur between leaving the function at await (4) and the statement after the call (6).

like image 110
Bergi Avatar answered Oct 29 '22 17:10

Bergi