Understanding Async/Await in JavaScript

Why do we need async/await in JavaScript? Async/await is a major source of confusion in JavaScript. People tend just to add and remove the async and await keywords until the code works (or seems to work). In this article, I want to explain what async/await actually means by developing the concept from vanilla, synchronous JavaScript to asynchronous JavaScript with the async/await syntax sugar. It is my personal take on the subject. I hope some will find it interesting. Disclaimer: I use Node.js as an example of a JavaScript runtime in this article. However, what's discussed also applies to other runtimes like web browser JS engines. All these environments follow similar architectures. Suppose you have a function that does some I/O. function handleRequest() { try { const data = doDatabaseQuery(); const fileName = doElasticsearchQuery(data); const result = readMyFile(fileName); return result; } catch (err) { return handleError(err); } } Here we assume that the query functions and the readMyFile functio https://lnkd.in/gkSCJnHx

To view or add a comment, sign in

Explore content categories