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