Promise (JavaScript)

A Promise represents a value that will be ready in the future, such as the result of a network request. It can be pending, fulfilled, or rejected.

JavaScript

Promises replaced messy nested callbacks. You attach .then() to handle success and .catch() to handle errors, and the code reads top to bottom.

async/await is built on top of promises and is usually the cleaner way to consume them.

Syntax

new Promise((resolve, reject) => { ... })

Examples

JavaScript

const p = new Promise(resolve => resolve("done"));
p.then(result => console.log(result));

Output:

done

Learn it in a lesson

See also

Back to the Dictionary