Promise.prototype.done = function(onResolved, onRejected){
this
.then(onResolved, onRejected)
.catch(function(err){
setTimeout(() => { throw err}, 0); //抛出一个全局错误
})
}
var p = ()=>new Promise((resolve, reject) => resolve('success'));
p()
.then(data => {console.log(data); x+1;})
.catch(err => {console.log(err.message); y+2;})
.then(data => console.log(data))
.done();
Promise.prototype.finally = function (callback) {
'use strict';
let P = this.constructor;
return this.then(
value => P.resolve(callback(value)),
err => P.resolve(callback(err))
);
};
var p = () => new Promise((resolve, reject) => resolve('success'));
p()
.then(data => {console.log(data); x+1;})
.catch(err => {console.log(err.message); y+2;})
.then(data => console.log(data))
.finally(data => console.log(data));
文章来源:http://blog.csdn.net/sinat_17775997/article/details/60581125