In the previous article, we reviewed our understanding of the Nodejs event loop. We understood how Nodejs uses multiple callback queues for different functions and pushes them back on the call stack in a particular priority. In the bonus section, I introduced the microtask queue which is responsible for handling functions deferred using the Promise API. Microtask queue has the highest priority over any other queue, also, it is divided into further two queues: One for the functions deferred using process.nextTick() and other for Promises.

In this article, we will understand what a promise is, what is it doing in the background, and understand its behavior.

Why do Promises exist?

Javascript became popular due to its asynchronous nature. Before promises, writing asynchronous code meant writing hugely nested callbacks (callbacks inside callbacks), this meant less intuitive code, longer debugging sessions, and huge technical debt. To tackle this issue Promises were introduced in ES6. The motivation for its introduction was to avoid callback hell and make asynchronous code more readable.

In a quest to make code more readable, javascript maintainers built an API that is now one of the core fundamentals of javascript.

#javascript #typescript #promises #nodejs #asynchronous

Mastering Promises
1.25 GEEK