// 原生实现promise基本功能,实现依次调用,eg:a().then() //不用考虑实现race all finally的复杂功能 function Promise(fn) { if (typeof fn !== 'function') { throw new Error('Resolver is not a Function') } else { this.callbacks = []; const resolve = val => { setTimeout(() => { this.callbacks.forEach(cb => { cb(val) }); }, 0) }; fn(resolve); } } Promise.prototype.then = function (newCallBack) { this.callbacks.push(newCallBack); }; const a = new Promise(function (resolve) { setTimeout(() => { resolve('执行函数参数') }, 2000) }) a.then(function (val) { console.log(val); });