Promise

Promise – Creates a Promise object. Many JavaScript functions already return a promise, and will therefore support this pattern without being wrapped manually in a Promise object.

Description

new Promise(function executor)

Parameters

Name Description Type Default Optional
executor function No
Executor parameters
resolve

A callable to resolve the Promise. The promise did what it was supposed to, and all is well.

reject

A callable to reject the Promise. Something went wrong.

Each of the executor functions may have one argument that will passed down to the callback.

Changelog

Version Description
ES 6 Introduced.

Examples

Example #1 – Promise example
new Promise((resolve, reject) => { // Randomly resolve or reject the promise. if (Math.random() >= 0.5) { resolve('resolved! :)'); } reject('rejected :('); }).then(result => { console.log(result); }).catch(result => { console.log(result); });
Example #2 – Late binding example
let mypromise = new Promise(resolve => { setTimeout(() => { resolve('resolved! :)'); }, 200); }); // Make a delay so the callback is executed some time after the actual promise was resolved. setTimeout(() => { mypromise.then(result => { console.log(result); // resolved! :) }); }, 1000); // As soon as the promise is resolved this will execute, which should happen after about 200ms. mypromise.then(result => { console.log(mypromise); // Promise {<resolved>} }); // First log entry. At this time the promise is still pending since it should take about 200ms to resolve. console.log(mypromise); // Promise {<pending>}

See also

External references