码迷,mamicode.com
首页 > 其他好文 > 详细

03回调函数

时间:2020-06-26 16:32:04      阅读:46      评论:0      收藏:0      [点我收藏+]

标签:console   col   promise   eth   request   turn   call   ons   lse   

//回调函数
doSomeThing(result => {
  doSomeThingElse(result, newResult => {
    doSomeThingThird(newResult, finalResult => {
      console.log(finalResult)
    }, errCallback)
  }, errCallback)
}, errCallback)

// promise嵌套
doSomeThing()
  .then(result => {
    return doSomeThingElse(result)
  })
  .then(newResult => {
    return doSomeThingThird(newResult)
  })
  .then(finalResult => {
    console.log(finalResult)
  })
  .catch(err => {
    console.log(err);

  })
//异常穿透,只需要指定一个catch



async function request() {
  try {
    const result = await doSomeThing()
    const newResult = await doSomeThingElse(result)
    const finalResult = await doSomeThingThird(newResult)
    console.log(‘finalResult: ‘, finalResult);
  } catch (error) { //异常穿透
    errCallback(error) 
  }

}
let errCallback = error => {
  console.log(error)
}

 

03回调函数

标签:console   col   promise   eth   request   turn   call   ons   lse   

原文地址:https://www.cnblogs.com/xiaoliziaaa/p/13195250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!