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