共计 669 个字符,预计需要花费 2 分钟才能阅读完成。
1、最基本的功能:
function Promise(fn){this.arr = [];
this.then = function(thParam){thParam(this.arr);
//return this;
};
var that = this;
function resolve(parm){that.arr.push(parm);
console.log(parm)
}
fn(resolve);
}
var p1 = new Promise(function(resolve){resolve("参数给 then");
});
p1.then(function(response){console.log(response)
});
2、链式调用
加上一个 return this 就可以了
function Promise(fn){this.arr = [];
this.then = function(thParam){thParam(this.arr);
return this;
};
var that = this;
function resolve(parm){that.arr.push(parm);
console.log(parm)
}
fn(resolve);
}
var p1 = new Promise(function(resolve){resolve("参数给 then");
});
p1.then(function(response){console.log(response)
}).then(function(response){console.log(response)
});
参考资料:手写一个 Promise
正文完
发表至: javascript
2019-07-15