JavaScript async (非同步) , sync (同步)
上篇Promise
生活範例:
- async(非同步) :
- 去醫院掛號
- 等護士叫,可以先去上廁所
護士叫了,我在過去
sync(同步) :
- 去醫院掛號
- 在櫃檯等,等到護士說好了
- 我才能去上廁所
實作解析:
另一個 Js 檔案(utils.js 是資料庫)
try()catch() 捕捉錯誤
語法:
try (用鑰匙開門){
小妹用自己鑰匙開自己房門成功
} catch(){
小妹用哥哥鑰匙開自己房門失敗
}
// foo 透過 async 回傳會得到一個 foo()
// foo() 是一個 promise
const foo = async()=>{
return {
a:1,
b:2
}
}
// 1. promise 版本
foo().then((data)=>{
}).catch(error=>{
})
// 2. async 版本
async()=>{
try {
const data2 = await foo ()
}catch(error){
return(error)
}
}
小叮嚀:
.then().catch()是Promise的用法, async 沒有 .then().catch(), 所以只好用 try() catch() 來捕捉錯誤
await(等待),外層一定要有 async(非同步)包裹著
Comments
Join the discussionNo comments yet. Be the first to comment.

