分析ES 6 Promise
990 ワード
簡単版ES 6文法
原コード対応
function _Promise(...args) {
this.status = "pending";
this.msg = "";
const process = args[0];
process(
(...args) => {
this.status = "resolve";
this.msg = args[0];
},
(...args) => {
this.status = "reject";
this.msg = args[0];
}
);
return this;
}
_Promise.prototype.then = function(...args) {
if (this.status == "resolve") {
args[0](this.msg);
}
if (this.status == "reject" && arguments[1]) {
args[1](this.msg);
}
};
//
const mm = new _Promise((resolve, reject) => {
resolve("123");
});
mm.then(
msg => {
console.log(msg);
console.log("ok!");
},
() => {
console.log("fail!");
}
);
Promise 01原コード対応
const test = new Promise((resolve, reject) => {
resolve(123)
})
test.then((msg) => {
console.log(msg)
console.log('ok')
} )
j