function Promise(executor) { // 在promise内部定义一个状态,当前promise的状态 let self = this; self.status = 'pending'; // 默认promise的状态 self.value = undefined; self.reason = undefined; function resolve(value) { if(self.status === 'pending'){ self.value = value; self.status = 'resolved'; } } function reject(reason) { if(self.status === 'pending'){ self.reason = reason; self.status = 'rejected'; } } executor(resolve, reject); } Promise.prototype.then = function (onFulfilled, onRejected) { let self = this; if(self.status === 'resolved'){ onFulfilled(self.value); } if(self.status === 'rejected'){ onRejected(self.reason); } }; let p = new Promise(function (resolve, reject) { resolve('成功啦') }); p.then((value)=>{ console.log('very good'); console.log(value); });
promise更多资料查询 https://promisesaplus.com