Skip to content

Latest commit

 

History

History
39 lines (27 loc) · 1.19 KB

Promise_Error_Handling.md

File metadata and controls

39 lines (27 loc) · 1.19 KB

Promise Error Handling

51 52 53

上图53产生结果为

caugh by .catch Error: Unauthorized access to the user data

if try catch in promise

54

Summary:

  • promise error couldn't catch by try-catch
  • Inside the promise, the promise.catch() method will catch the error caused by the throw statement and reject().
  • If an error occurs and you don’t have the promise.catch() method, the JavaScript engine issues a runtime error and stops the program.
  • try-catch will catch error inside a promise, and not delegated it out the promise. try-catch并不会把错误再往上传递出去
  • 下方效果相同, 区别是await会阻塞等待, 但是promise.then不会:
// 等待 b() 执行完, 才执行后续的代码
const a = await b()

// 该函数执行后, 马上执行后面的代码, 再回头异步完成执行 then() 里面的方法
b()
 .then(a => ...)
 .catch(err => ...)
  • 如果一条链路对return的值有要求, 要用await方法去等待

reference

blog