Skip to content
Lydia Hallie theavocoder
Course contents
Complete JavaScript / Lesson 12

Async and await

An async function is syntactic sugar over promises, the code looks much cleaner. Its asynchronous code looks and behaves a little more like synchronous code. With async/await, you really wait for one promise to resolve before moving to the next line.

Async/await diagram 1Async/await diagram 2Async/await diagram 3Async/await diagram 4

We can access the await keyword in a function if it has the keyword async before it. Any async function returns a promise implicitly, and the resolve value of the promise will be whatever you return from the function, which is “data” in our case.
console.log(awaitcallPromise()) basically means: wait until the callPromise promise has resolved, then console.log gets called that prints the value that is returned. It pauses the execution of your method until the value from the promise is available. If you don’t await, you’ll get the promise instead of the value you expect!

Async/await and promises are not logically equivalent! The async/await loads the results in sequence, while the promise example loads in parallel. However, async/await and promises are the same thing under the hood.

References and further reading