JavaScript Promises

Pascual Vila
Senior Developer // Code Syllabus
JavaScript is single-threaded. To prevent long-running tasks from freezing the browser, we use Asynchronous Programming. Promises are the modern foundation of async JS.
What is a Promise?
A Promise is an object representing the eventual completion or failure of an asynchronous operation. Think of it like a restaurant buzzer: you place an order, they give you a buzzer (the Promise). You can keep chatting with your friends (synchronous code keeps running), and eventually, the buzzer rings (the Promise resolves).
Then and Catch
Once a Promise is created, you need to handle its outcome. We use the.then() method to schedule a callback for when the Promise is fulfilled. For errors, we chain a.catch() method. This prevents the "Callback Hell" (Pyramid of Doom) common in older JS code.
Async / Await
Introduced in ES2017, async/await provides a more readable, synchronous-looking syntax for working with Promises. By placing async before a function, it automatically returns a Promise. Inside it, await pauses the function execution until the Promise settles.