Code coverage of await statements in Javascript

KMA Badshah
Mar 2, 2021

--

Some people get confused about how much code an await statement actually covers. That is, how much code has to wait before the await statement resolves. The answer is pretty simple. Only the code that is after the await statement and part of the async function get’s blocked until the await statement resolves. Let’s see an example.

In the picture above we can see the first log executes, then because of the await statement the entire async function gets blocked from further execution before 1 second . But that doesn’t block the main thread. The interpreter jumps on to the second function and executes the log statement there. And after that, the promise in the first function resolves, the log statement gets printed, and the interpreter continues to log the very last statement of the first function.

--

--

KMA Badshah
0 Followers

Passionate web and mobile developer with over 2 years of experience in building anything from SPA's to OS agnostic mobile apps.