Why Promises?
Promises are the cleanest way to handle scenarios where our code needs to interact with external systems.
Almost every time you use a Promise in the real world, you are handling some form of I/O (Input/Output).
What is I/O?
I/O refers to any situation where your program needs to reach outside the isolated, highly predictable world of its own local variables and CPU processing to interact with the chaotic outside world.
Common examples of I/O include:
- Making HTTP network requests to a server
- Reading or writing files to a hard drive
- Sending data to a database
- Interacting with peripheral hardware (like a Bluetooth device or a webcam)
Because I/O operations rely on external systems (networks, disks, other servers), they are inherently slow and unpredictable.
The Promise Solution
Promises help us perform I/O without forcing our entire program to freeze up. For example, JavaScript’s built-in fetch() function—used to make network requests—returns a Promise immediately, allowing your code to move on while the browser handles the actual network traffic in the background.