JS中异步调用可以转化为同步调用吗?谢谢哈~

functionwait(){
constp=()=>({
value:newPromise(resolve=>setTimeout(()=>resolve(),3000))
})
letstate={
next:()=>{
state.next=programPart
returnp()
}
}
functionprogramPart(){
console.log('unlocked1')
state.next=programPart2
returnp()
}
functionprogramPart2(){
console.log('unlocked2')
console.log('it\'ssync!!')
return{
value:void0
}
}
returnstate
}
这个函数该如何调用?
梦里花落0921
浏览 365回答 2
2回答

芜湖不芜

varstate=wait()state.next().value.then(()=>state.next().value).then(()=>state.next().value).then(()=>console.log(state.next()))

手掌心

可以使用async函数来实现视觉上的同步调用。async是Generator函数的语法糖。constfn=asyncfunction(){letstate=wait();awaitstate.next().value;awaitstate.next().value;returnstate.next().value;}fn()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript