MENU
Promise.resolve() and Promise.reject()
The static function Promise.resolve(value) or Promise.reject(value) returns a promise that is resolved or rejected respectively, with the given value.
Promise.reject(100).then(
console.log,
n => { console.log(n * 2) }
);200
Passing a Promise object to Promise.resolve() is identical to calling the promise directly.
var p = new Promise(resolve => {
resolve(100);
});
console.log(Promise.resolve(p) === p);
Promise.resolve(p).then(
console.log,
n => { console.log(n * 2) }
);true
100
Passing to Promise.resolve() a thenable (an object with a 'method' taking two callbacks) is identical to calling a promise with the two callbacks in the thenable.
var thenable = {
then: (onFulfill, onReject) => { onFulfill('fulfilled'); }
};
Promise.resolve(thenable).then(console.log);
var p = new Promise(resolve => { resolve('fulfilled'); }); // just identical to the above
p.then(console.log);fulfilled
fulfilled