Both Promise.resolve()
and Promise.reject()
accept non-promise thenables as arguments.
When passed a non-promise thenable, these methods create a new promise that is called after the then()
function.
A non-promise thenable is created when an object has a then()
method that accepts a resolve
and a reject
argument, like this:
let thenable = { then: function(resolve, reject) { resolve(42); } };
You can call Promise.resolve()
to convert thenable
into a fulfilled promise:
let thenable = {/*from www . j a v a2 s . co m*/ then: function(resolve, reject) { resolve(42); } }; let p1 = Promise.resolve(thenable); p1.then(function(value) { console.log(value); // 42 });
We can use Promise.resolve()
to create a rejected promise from a thenable:
let thenable = {/*from w w w.j a v a2s . c o m*/ then: function(resolve, reject) { reject(42); } }; let p1 = Promise.resolve(thenable); p1.catch(function(value) { console.log(value); // 42 });