Promises in JavaScript are like the friend who says they’ll help you move and shows up three hours late. In this article, we unpack promises, async/await, and how to stop chaining .then() like it’s a bad habit.
🤝 The Problem: Asynchronous Code
JavaScript is inherently single-threaded. This means it can only execute one thing at a time. If you run a long operation, like fetching data from a server (an I/O operation), the entire browser or Node.js application freezes, waiting for the response. This is called blocking code.
To avoid this, JavaScript handles long-running operations asynchronously. The operation is started, and the main thread moves on to execute other code, rather than waiting. When the asynchronous operation finally finishes, a notification is sent back.
Before Promises, developers used callbacks—functions passed into an asynchronous function to be executed later. This often led to the dreaded “Callback Hell” or “Pyramid of Doom”: deeply nested, unreadable code that was a nightmare to maintain.