functionwait(){constp=()=>({value:newPromise(resolve=>setTimeout(()=>resolve(),3000))})letstate={next:()=>{state.next=programPartreturnp()}}functionprogramPart(){console.log('unlocked1')state.next=programPart2returnp()}functionprogramPart2(){console.log('unlocked2')console.log('it\'ssync!!')return{value:void0}}returnstate}这个函数该如何调用?
2 回答
不负相思意
TA贡献1777条经验 获得超10个赞
varstate=wait()state.next().value.then(()=>state.next().value).then(()=>state.next().value).then(()=>console.log(state.next()))
慕运维8079593
TA贡献1876条经验 获得超5个赞
可以使用async函数来实现视觉上的同步调用。async是Generator函数的语法糖。constfn=asyncfunction(){letstate=wait();awaitstate.next().value;awaitstate.next().value;returnstate.next().value;}fn()
添加回答
举报
0/150
提交
取消