第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

在nodejs中使用嵌套承諾處理錯(cuò)誤

在nodejs中使用嵌套承諾處理錯(cuò)誤

慕桂英3389331 2022-01-07 21:07:25
當(dāng)嵌套的 promise 塊中發(fā)生錯(cuò)誤時(shí),需要在最后一個(gè)外部 catch 塊中捕獲所有錯(cuò)誤。let docs = {  total: 0,  total_downloaded: 0,  plan_type: null,};Document.findAll({  where: {    report_request_id: req.params.requestId  },  attributes: ["id", "folder_name", "total_file"],})  .then(documents => {    documents.forEach(document => {        docs.total += 1;        if (document.get("status") == 1) {          docs.total_downloaded += 1;        }    });  })  .then(function() {    Request.findOne({      where: {        id: req.params.requestId      }    })      .then(request => {        //Suppose I got error here        docs.plan_type = request.plan_type;      })      .catch(err => {        // Block A        throw err;      });  })  .then(function() {    res.status(200).send(docs);  })  .catch(err => {    // Block B    res.status(400).send(err);  });截至目前,即使我在 catch 塊 A 中遇到錯(cuò)誤,我每次都會成功(200)
查看完整描述

2 回答

?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊

你錯(cuò)過了return鏈接承諾的聲明Request.findOne()。


let docs = {

  total: 0,

  total_downloaded: 0,

  plan_type: null,

};


Document.findAll({

  where: {

    report_request_id: req.params.requestId

  },

  attributes: ["id", "folder_name", "total_file"],

})

  .then(documents => {

    documents.forEach(document => {

        docs.total += 1;


        if (document.get("status") == 1) {

          docs.total_downloaded += 1;

        }

    });

  })

  .then(function() {

    return Request.findOne({

      where: {

        id: req.params.requestId

      }

    })

      .then(request => {

        //Suppose I got error here

        docs.plan_type = request.plan_type;

      })

      .catch(err => {

        // Block A

        throw err;

      });

  })

  .then(function() {

    res.status(200).send(docs);

  })

  .catch(err => {

    // Block B

    res.status(400).send(err);

  });


查看完整回答
反對 回復(fù) 2022-01-07
?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊

您可以使用 Promises 或 async/await 在一個(gè) catch 塊中捕獲錯(cuò)誤。您可以專門在每個(gè)使用 promise 的 await 函數(shù)中拋出錯(cuò)誤。就像是。


const a = new Promise((resolve, reject) => {

    try {

        // do something

        resolve(output);

    } catch (error) {

        reject(error);

    }

});


const b = //... similar to a();

const c = //... similar to a();


const main = async () => {

    try {

        const f = await a();

        const i = await b(f); // if data from a is to be passed to b

        const y = await c(i); // if data from a is to be passed to b

        return y;

    } catch(error){

        // catch all errors

        throw new Error( error );

    }

}


main().then(( data )=>{

    // on success do something with data


}).catch((error)=>{

    //catch all errors and do something


});


查看完整回答
反對 回復(fù) 2022-01-07
  • 2 回答
  • 0 關(guān)注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號