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
try{module.exports=Promise}catch(e){}functionPromise(executor){varself=thisself.status='pending'self.onResolvedCallback=[]self.onRejectedCallback=[]functionresolve(value){if(valueinstanceofPromise){returnvalue.then(resolve,reject)}setTimeout(function(){// 异步执行所有的回调函数if(self.status==='pending'){self.status='resolved'self.data=valuefor(vari=0;i<self.onResolvedCallback.length;i++){self.onResolvedCallback[i](value)}}})}functionreject(reason){setTimeout(function(){// 异步执行所有的回调函数if(self.status==='pending'){self.status='rejected'self.data=reasonfor(vari=0;i<self.onRejectedCallback.length;i++){self.onRejectedCallback[i](reason)}}})}try{executor(resolve,reject)}catch(reason){reject(reason)}}functionresolvePromise(promise2,x,resolve,reject){varthenvarthenCalledOrThrow=falseif(promise2===x){returnreject(newTypeError('Chaining cycle detected for promise!'))}if(xinstanceofPromise){if(x.status==='pending'){//because x could resolved by a Promise Objectx.then(function(v){resolvePromise(promise2,v,resolve,reject)},reject)}else{//but if it is resolved, it will never resolved by a Promise Object but a static value;x.then(resolve,reject)}return}if((x!==null)&&((typeofx==='object')||(typeofx==='function'))){try{then=x.then//because x.then could be a getterif(typeofthen==='function'){then.call(x,functionrs(y){if(thenCalledOrThrow)returnthenCalledOrThrow=truereturnresolvePromise(promise2,y,resolve,reject)},functionrj(r){if(thenCalledOrThrow)returnthenCalledOrThrow=truereturnreject(r)})}else{resolve(x)}}catch(e){if(thenCalledOrThrow)returnthenCalledOrThrow=truereturnreject(e)}}else{resolve(x)}}Promise.prototype.then=function(onResolved,onRejected){varself=thisvarpromise2onResolved=typeofonResolved==='function' ? onResolved : function(v){returnv}onRejected=typeofonRejected==='function' ? onRejected : function(r){throwr}if(self.status==='resolved'){returnpromise2=newPromise(function(resolve,reject){setTimeout(function(){// 异步执行onResolvedtry{varx=onResolved(self.data)resolvePromise(promise2,x,resolve,reject)}catch(reason){reject(reason)}})})}if(self.status==='rejected'){returnpromise2=newPromise(function(resolve,reject){setTimeout(function(){// 异步执行onRejectedtry{varx=onRejected(self.data)resolvePromise(promise2,x,resolve,reject)}catch(reason){reject(reason)}})})}if(self.status==='pending'){// 这里之所以没有异步执行,是因为这些函数必然会被resolve或reject调用,而resolve或reject函数里的内容已是异步执行,构造函数里的定义returnpromise2=newPromise(function(resolve,reject){self.onResolvedCallback.push(function(value){try{varx=onResolved(value)resolvePromise(promise2,x,resolve,reject)}catch(r){reject(r)}})self.onRejectedCallback.push(function(reason){try{varx=onRejected(reason)resolvePromise(promise2,x,resolve,reject)}catch(r){reject(r)}})})}}Promise.prototype.catch=function(onRejected){returnthis.then(null,onRejected)}Promise.deferred=Promise.defer=function(){vardfd={}dfd.promise=newPromise(function(resolve,reject){dfd.resolve=resolvedfd.reject=reject})returndfd}
functionrequest(url,options={},callback){constfetchPromise=fetch(url,options).then(response=>response.json());letabort;constabortPromise=newPromise((resolve,reject)=>{abort=()=>{reject(Error('abort'));};});Promise.race([fetchPromise,abortPromise]).then(({ data })=>{callback(data);}).catch(()=>{});returnabort;}useEffect(()=>{constabort=request('https://cnodejs.org/api/v1/topic/5433d5e4e737cbe96dcef312',{},setData);return()=>{abort();};});
Pomise在涉及模式中称之为揭示构造函数,因为生成的一个Promise的状态只会由内部函数决定,这里我们就不详细说规范了,这次就实现一个Promise类来学习:
别人的实现方式
测试
如何判断我们实现的Promise符合规范,可以使用脚本来验证,使用方式如下:
问题
reject
和resolve
的实现是异步的,在node
和brower
上用哪个实现比较好,改写下实现的代码?好文章及其对应的源码
The text was updated successfully, but these errors were encountered: