Synchronous Vs. Asynchronous
Imagine sitting at a restaurant, where the waiter won’t take the order of the next table until your food is cooked and served. This is what synchronous operations are like. Each process waits for the preceding one to finish before it starts, creating a potential bottleneck in execution.
However, callbacks can lead to what’s known as “callback hell”, a situation where callbacks are nested within callbacks, leading to unreadable and unmaintainable code.
To mitigate callback hell, ES6 introduced Promises, an object that may produce a single value sometime in the future. Promises can be in one of three states: pending, fulfilled, or rejected. They also allow chaining, reducing the complexity of asynchronous code.
Building upon Promises, ES8 introduced async/await, a syntactic sugar that makes asynchronous code look and behave like synchronous code, making it easier to write and understand. It’s essentially a way to write promises that appear to block the execution, but without the blocking behavior.
The Challenges and Solutions