Promise 链式then、catch的使用,resolve 在构造 的使用
const myPromise = new Promise((resolve, reject) => {
if (Math.random() > 0) {
console.log('resolving the promise ...');
resolve('Hello, Positive :)');
}
reject(new Error('No place for Negative here :('));
});
const Fulfilled = (fulfilledValue) => console.log(fulfilledValue);
const Rejected = (error) => console.log(error);
myPromise.then(Fulfilled, Rejected);
const r = myPromise.then((fulfilledValue) => {
console.log(fulfilledValue);
return "fulfiled success ";
}).then(msg=>console.log(msg+" done ")).catch(err => console.log(err));
console.log(r.constructor.name)
Output:
resolving the promise ... Promise Hello, Positive :) Hello, Positive :) fulfiled success done