AlphaCamp学期三A5作业Callback练习

原始程式码

const step0 = 'step 0 - start'const step1 = 'step 1 - run'const step2 = 'step 2 - run'const stepDone = 'step Done'// @TODO:请修改以下函式const start = (callback) => {  setTimeout(() => {    console.log(step0)    callback()  }, 500)}// @TODO:请修改以下函式const stepEvent1 = () => {  return (callback) => {    console.log(step1)  }}// @TODO:请修改以下函式const stepEvent2 = () => {   () => {    setTimeout(() => {      console.log(step2)    }, 500)  }}const stepEventDone = () => {  return console.log(stepDone)}// start(stepEvent1)stepEvent2()

预期结果

step 0 - startstep 1 - runstep 2 - runstep Done

解题历程

由内层往外层

由stepEventDone往回推

完成结果如下

const step0 = 'step 0 - start'const step1 = 'step 1 - run'const step2 = 'step 2 - run'const stepDone = 'step Done'//start(stepEvent1)const start = (callback) => {  setTimeout(() => {    console.log(step0)    // @TODO:    callback()(stepEvent2)//stepEvent1()(stepEvent2)  }, 500)}const stepEvent1 = () => {  return (callback) => {    // @TODO:    console.log(step1)    callback()()//stepEvent2()()  }}const stepEvent2 = () => {  return () => {    setTimeout(() => {      // @TODO      console.log(step2)      stepEventDone()    }, 500)  }}const stepEventDone = () => {  return console.log(stepDone)}// stepEvent2()()// stepEvent1()(stepEvent2)start(stepEvent1)

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章