node.js async/await 處理異常?

一、我只是console一下,我不處理。

async function getData(){
  const a = await someFunction().catch((error)=>console.log(error));
  const b = await someOtherFunction().catch((error)=>console.log(error));
  if(a && b ) console.log("some result")
}

二、約定法則

const go = async () => {
    const readFileResult = await sureThing(readFile('some.json'));
    if (readFileResult.ok) {
        const {
            ok,
            error,
            data
        } = await sureThing(parseJSON(data));
        if (ok) {
            // use our data
        } else {
            return {
                ok,
                error
            };
        }
    } else {
        return readFileResult;
    }
};
try{
let a = await func();
//如果resolve,此處的a爲resolve的值
}catch(e){
//如果reject了,此處的e爲reject的錯誤
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章