为Promise添加finally方法支持,把小程序函数变成promise函数
时间:2020-06-22 12:32:43
收藏:0
阅读:107
// 为Promise添加finally方法支持
Promise.prototype.finally = function (callback) {
let P = this.constructor;
return this.then(
value => P.resolve(callback()).then(() => value),
reason => P.resolve(callback()).then(() => {
throw reason
})
);
};
// 把小程序函数变成promise函数
const promisify = (fn) => {
return (options, ...params) => {
return new Promise((resolve, reject) => {
fn(Object.assign({}, options, {
success: resolve,
fail: reject
}), ...params);
});
}
};
评论(0)