Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

异步编程-Promise 题目 6 - 2 #148

Open
leslie1943 opened this issue Jan 14, 2021 · 0 comments
Open

异步编程-Promise 题目 6 - 2 #148

leslie1943 opened this issue Jan 14, 2021 · 0 comments

Comments

@leslie1943
Copy link
Owner

Promise 6 - 1

async function async1() {
  try {
    await Promise.reject('error!!!')
  } catch (e) {
    console.log(e)
  }
  console.log('async1')
  return Promise.resolve('async1 success')
}
async1().then((res) => console.log(res))
console.log('script start')

// script start
// error!!!
// async1
// async1 success

过程讲解

  • 如果想要使得错误的地方不影响 async 函数后续的执行的话, 可以使用 try catch
  1. 程序执行, 调用 async1执行, 遇到await,暂停, 跳出函数.
  2. 输出 script start
  3. 执行微任务try, 遇到 Promise.reject('error!!!'),跳转到 catch去执行
  4. 然后继续执行 async1, 执行 return
  5. 执行.then() 输出 返回结果
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant