How Promises Work in JavaScript

Description:

Promises help handle asynchronous operations, replacing callback-based programming.

Explanation:

A promise represents a value that will be available in the future. It has three states: pending, resolved, and rejected.

Example:

const myPromise = new Promise((resolve, reject) => {
    setTimeout(() => resolve("Promise resolved!"), 2000);
});

myPromise.then(result => console.log(result)); // Output after 2 sec: Promise resolved!