手写一个 promise 2019-07-09
作者:
八喜_ne | 来源:发表于
2019-07-09 11:20 被阅读0次function Promise(executor) {
let self = this;
self.status = 'peding';
self.value = null;
self.reason = null;
function resolve(value) {
if (self.status === 'peding') {
self.value = value;
self.status = 'resolve';
}
}
function reject(reason) {
if (self.status === 'peding') {
self.reason = reason;
self.status = 'rejectd';
}
}
// 这里对异常进行处理
try {
exector(resolve, reject);
} catch (e) {
reject(e);
}
}
Promise.prototype.then = function(onFulfilled, onReject) {
let self = this;
if (self.status === 'resloved') {
onFulfilled(self.value);
}
if (self.status === 'rejected') {
onReject(self.reason);
}
};
module.exports = Promise;
本文标题:手写一个 promise 2019-07-09
本文链接:https://www.haomeiwen.com/subject/pxztkctx.html
网友评论