If you’ve had the chance to observe modern JavaScript code, there are high chances that you’ve seen the async and await syntax somewhere.

Async/await is arguably one of the best-received new additions to the language. Async/await makes the asynchronous code appear and behave like synchronous code. Async/await are promise-based.

Before we jump into async/await, we must understand what promises are and the role they play.

Promises

Promise is an object representing the eventual completion or failure of an asynchronous operation.

Why do we need the asynchronous code, anyway?

JavaScript is a single-threaded language — which means JavaScript can only do one thing at once. Imagine calling our API synchronously and blocking the entire thread for the API call duration — our users would have to wait 30 seconds or as long it takes for the network request to resolve — a big no-no!

In case you’re interested to learn more — here’s a more in-depth explanation about asynchronous programming with JavaScript.

The way we used to handle asynchronous JavaScript code was via callbacks. Chances are high that you’ve come across callbacks.

#javascript #programming #web development #promises

How to Improve Your Asynchronous JavaScript Code with Async and Await
21.10 GEEK