promi this

更新时间:2022-12-29 05:31:24 阅读: 评论:0


2022年12月29日发(作者:大风歌刘邦)

Promi捕获错误

1.捕获执⾏器异常

constMyPromi=require('./myPromi')

letpro=newMyPromi((resolve,reject)=>{

thrownewError('executorerror')

resolve('hello')

})

(value=>{

(value)

},reason=>{

(e)

})

constPENDING='pending'//等待

constFULFILLED='fulfilled'//成功

constREJECTED='rejected'//失败

classMyPromi{

constructor(executor){

//捕获执⾏器错误

try{

executor(e,)

}catch(e){

(e)

}

}

status=PENDING

value=undefined

reason=undefined

successCallback=[]

failCallback=[]

resolve=value=>{

if(!==PENDING)return

//将状态修改为成功

=FULFILLED

//保存成功之后的值

=value

//sCallback&&sCallback()

while(){

()()

}

}

reject=reason=>{

if(!==PENDING)return

=REJECTED

=reason

//llback&&llback()

while(){

()()

}

}

then(successCallback,failCallback){

letpromi2=newMyPromi((resolve,reject)=>{

if(===FULFILLED){

tTimeout(()=>{

letx=successCallback()

//resolve(x)

resolvePromi(promi2,x,resolve,reject)

},0)

}elif(===REJECTED){

failCallback()

}el{

//等待

//讲成功回调和失败回调进⾏存储

(successCallback)

(failCallback)

}

})

})

returnpromi2

}

}

functionresolvePromi(promi2,x,resolve,reject){

if(promi2===x){

returnreject(newTypeError('Chainingcycledetectedforpromi#'))

}

if(xinstanceofMyPromi){

//(value=>resolve(value),reason=>reject(reason))

(resolve,reject)

}el{

resolve(x)

}

}

s=MyPromi

函数抛出异常

letpro=newMyPromi((resolve,reject)=>{

//thrownewError('executorerror')

resolve('hello')

})

(value=>{

(value)

thrownewError('thenerror')

},reason=>{

(e)

}).then(value=>{

(value)

},reason=>{

(e)

})

constPENDING='pending'//等待

constFULFILLED='fulfilled'//成功

constREJECTED='rejected'//失败

classMyPromi{

constructor(executor){

//捕获执⾏器错误

try{

executor(e,)

}catch(e){

(e)

}

}

status=PENDING

value=undefined

reason=undefined

successCallback=[]

failCallback=[]

resolve=value=>{

if(!==PENDING)return

//将状态修改为成功

=FULFILLED

//保存成功之后的值

=value

//sCallback&&sCallback()

while(){

()()

}

}

reject=reason=>{

if(!==PENDING)return

=REJECTED

=reason

=reason

//llback&&llback()

while(){

()()

}

}

then(successCallback,failCallback){

letpromi2=newMyPromi((resolve,reject)=>{

if(===FULFILLED){

tTimeout(()=>{

try{

letx=successCallback()

//判断x的值是普通值还是promi对象

//如果是普通值,直接调⽤resolve

//如果是promi对象,查看promi对象返回的结果

//再根据promi对象返回的结果,决定调⽤resolve还是reject

resolvePromi(promi2,x,resolve,reject)

}catch(e){

reject(e)

}

},0)

}elif(===REJECTED){

failCallback()

}el{

//等待

//讲成功回调和失败回调进⾏存储

(successCallback)

(failCallback)

}

})

returnpromi2

}

}

functionresolvePromi(promi2,x,resolve,reject){

if(promi2===x){

returnreject(newTypeError('Chainingcycledetectedforpromi#'))

}

if(xinstanceofMyPromi){

//(value=>resolve(value),reason=>reject(reason))

(resolve,reject)

}el{

resolve(x)

}

}

s=MyPromi

3.异步情况的处理

constPENDING='pending'//等待

constFULFILLED='fulfilled'//成功

constREJECTED='rejected'//失败

classMyPromi{

constructor(executor){

//捕获执⾏器错误

try{

executor(e,)

}catch(e){

(e)

}

}

status=PENDING

value=undefined

reason=undefined

successCallback=[]

failCallback=[]

resolve=value=>{

if(!==PENDING)return

//将状态修改为成功

=FULFILLED

=FULFILLED

//保存成功之后的值

=value

//sCallback&&sCallback()

while(){

()()

}

}

reject=reason=>{

if(!==PENDING)return

=REJECTED

=reason

//llback&&llback()

while(){

()()

}

}

then(successCallback,failCallback){

letpromi2=newMyPromi((resolve,reject)=>{

if(===FULFILLED){

tTimeout(()=>{

try{

letx=successCallback()

//判断x的值是普通值还是promi对象

//如果是普通值,直接调⽤resolve

//如果是promi对象,查看promi对象返回的结果

//再根据promi对象返回的结果,决定调⽤resolve还是reject

resolvePromi(promi2,x,resolve,reject)

}catch(e){

reject(e)

}

},0)

}elif(===REJECTED){

tTimeout(()=>{

try{

letx=failCallback()

//判断x的值是普通值还是promi对象

//如果是普通值,直接调⽤resolve

//如果是promi对象,查看promi对象返回的结果

//再根据promi对象返回的结果,决定调⽤resolve还是reject

resolvePromi(promi2,x,resolve,reject)

}catch(e){

reject(e)

}

},0)

}el{

//等待

//讲成功回调和失败回调进⾏存储

(()=>{

tTimeout(()=>{

try{

letx=successCallback()

//判断x的值是普通值还是promi对象

//如果是普通值,直接调⽤resolve

//如果是promi对象,查看promi对象返回的结果

//再根据promi对象返回的结果,决定调⽤resolve还是reject

resolvePromi(promi2,x,resolve,reject)

}catch(e){

reject(e)

}

},0)

})

(()=>{

tTimeout(()=>{

try{

letx=failCallback()

//判断x的值是普通值还是promi对象

//如果是普通值,直接调⽤resolve

//如果是promi对象,查看promi对象返回的结果

//再根据promi对象返回的结果,决定调⽤resolve还是reject

resolvePromi(promi2,x,resolve,reject)

}catch(e){

reject(e)

}

}

},0)

})

}

})

returnpromi2

}

}

functionresolvePromi(promi2,x,resolve,reject){

if(promi2===x){

returnreject(newTypeError('Chainingcycledetectedforpromi#'))

}

if(xinstanceofMyPromi){

//(value=>resolve(value),reason=>reject(reason))

(resolve,reject)

}el{

resolve(x)

}

}

s=MyPromi

letpro=newMyPromi((resolve,reject)=>{

//thrownewError('executorerror')

//resolve('hello')

//reject('失败')

tTimeout(()=>{

resolve('成功。。。')

},2000)

})

(value=>{

(value+'1')

//thrownewError('thenerror')

return'aaa'

},reason=>{

(reason+'2')

return10000

}).then(value=>{

(value+'3')

})

本文发布于:2022-12-29 05:31:24,感谢您对本站的认可!

本文链接:http://www.wtabcd.cn/fanwen/fan/90/51442.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:promise this
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图