You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
2.2.7.1. If either onFulfilled or onRejected returns a value x, run the Promise Resolution Procedure [[Resolve]](promise2, x).
resolutionProcedure 方法的实现规则如下,只需要将规则翻译成代码即可:
To run [[Resolve]](promise, x), perform the following steps:
2.3.1. If promise and x refer to the same object, reject promise with a TypeError as the reason.
2.3.2. If x is a promise, adopt its state:
2.3.2.1. If x is pending, promise must remain pending until x is fulfilled or rejected.
2.3.2.2. If/when x is fulfilled, fulfill promise with the same value.
2.3.2.3. If/when x is rejected, reject promise with the same reason.
2.3.3. Otherwise, if x is an object or function,
2.3.3.1. Let then be x.then.
2.3.3.2. If retrieving the property x.then results in a thrown exception e, reject promise with e as the reason.
2.3.3.3. If then is a function, call it with x as this, first argument resolvePromise, and second argument rejectPromise, where:
2.3.3.3.1. If/when resolvePromise is called with a value y, run [[Resolve]](promise, y).
2.3.3.3.2. If/when rejectPromise is called with a reason r, reject promise with r.
2.3.3.3.3. If both resolvePromise and rejectPromise are called, or multiple calls to the same argument are made, the first call takes precedence, and any further calls are ignored.
2.3.3.3.4. If calling then throws an exception e,
2.3.3.3.4.1. If resolvePromise or rejectPromise have been called, ignore it.
2.3.3.3.4.2. Otherwise, reject promise with e as the reason.
2.3.3.4. If then is not a function, fulfill promise with x.
2.3.4. If x is not an object or function, fulfill promise with x.
functionresolutionProcedure(promise2,x,resolve,reject){// 2.3.1. x 不能和 promise2 相同,避免循环引用if(promise2===x){returnreject(newTypeError("Error"));}// 2.3.2. x 是一个Promise对象,接受 x 的状态if(xinstanceofMyPromise){// 2.3.2.x. x 状态为 pending 需要继续等待,否则取 x 的状态if(x.status===PENDING){x.then(function(value){// 再次调用该函数确认 x resolve 的参数是什么类型// 如果是基本类型就再次 resolve,并把值传给下个 thenresolutionProcedure(promise2,value,resolve,reject);},reject);}else{x.then(resolve,reject);}return;}// 2.3.3. x 是对象或者函数if(x!==null&&(typeofx==="object"||typeofx==="function")){letcalled=false;// 2.3.3.2. try{letthen=x.then;if(typeofthen==='function'){then.call(x,y=>{if(called)return;called=true;resolutionProcedure(promise2,y,resolve,reject);},r=>{if(called)return;called=true;reject(r);});}else{// 2.3.3.4.resolve(x);}}catch(e){// 2.3.3.3.4.1.if(called)return;called=true;// 2.3.3.3.4.2.reject(e);}}else{// 2.3.4.resolve(x);}}
使用resolutionProcedure 方法替换then判断 x 是否为Promise对象的代码如下:
简单实现 Promise
根据 Promise/A+ 规范,实现一个简单的 Promise 对象
1. 定义 Promise 对象的三种状态
2. 实现一个构造函数
3. 实现构造函数中的 resolve 和 reject 方法
判断状态是否为 Pending,是则改变为相应的状态,并调用回调函数。
4. 实现 then 方法
1)
then
方法应该定义在原型链上;2)
then
方法返回一个新的 Promise 对象;3)根据 Promise/A+ 规范:
5. 实现 resolutionProcedure 方法
实现不同 Promise 实现之间的无缝可交互。
根据 Promise/A+ 规范:
resolutionProcedure
方法的实现规则如下,只需要将规则翻译成代码即可:使用
resolutionProcedure
方法替换then
判断 x 是否为Promise对象的代码如下:6. 测试
至此,已经按照规范实现了一个简单的 Promise,完整代码。
使用Promises/A+ Compliance Test Suite进行测试:
运行:
参考:
剖析Promise内部结构,一步一步实现一个完整的、能通过所有Test case的Promise类
The text was updated successfully, but these errors were encountered: