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

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

使用bluebird承諾進行異步異常處理

使用bluebird承諾進行異步異常處理

慕慕森 2019-08-14 15:36:07
使用bluebird承諾進行異步異常處理處理此方案的最佳方法是什么。我處于受控環(huán)境中,我不想崩潰。var Promise = require('bluebird');function getPromise(){     return new Promise(function(done, reject){         setTimeout(function(){                 throw new Error("AJAJAJA");         }, 500);     });}var p = getPromise();     p.then(function(){         console.log("Yay");     }).error(function(e){         console.log("Rejected",e);     }).catch(Error, function(e){         console.log("Error",e);     }).catch(function(e){         console.log("Unknown", e);     });從setTimeout中拋出時,我們總是得到:$ node bluebird.js c:\blp\rplus\bbcode\scratchboard\bluebird.js:6                 throw new Error("AJAJAJA");                       ^Error: AJAJAJA     at null._onTimeout (c:\blp\rplus\bbcode\scratchboard\bluebird.js:6:23)     at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)如果拋出發(fā)生在setTimeout之前,那么bluebirds catch會把它拿起來:var Promise = require('bluebird');function getPromise(){     return new Promise(function(done, reject){         throw new Error("Oh no!");         setTimeout(function(){             console.log("hihihihi")         }, 500);     });}var p = getPromise();     p.then(function(){         console.log("Yay");     }).error(function(e){         console.log("Rejected",e);     }).catch(Error, function(e){         console.log("Error",e);     }).catch(function(e){         console.log("Unknown", e);     });結(jié)果是:$ node bluebird.jsError [Error: Oh no!]哪個好 - 但是如何在節(jié)點或瀏覽器中處理這種性質(zhì)的流氓異步回調(diào)。
查看完整描述

2 回答

?
開滿天機

TA貢獻1786條經(jīng)驗 獲得超13個贊

現(xiàn)在我知道promise在異步回調(diào)中沒有捕獲錯誤。這是我測試的3個例子。

注意:拒絕呼叫后,功能將繼續(xù)運行。

示例1:拒絕,然后在promise構(gòu)造函數(shù)回調(diào)中拋出錯誤

示例2:拒絕,然后在setTimeout異步回調(diào)中拋出錯誤

示例3:拒絕,然后返回setTimeout異步回調(diào)以避免崩潰

// Caught// only error 1 is sent// error 2 is reached but not send reject againnew Promise((resolve, reject) => {
  reject("error 1"); // Send reject
  console.log("Continue"); // Print 
  throw new Error("error 2"); // Nothing happen})
  .then(() => {})
  .catch(err => {
    console.log("Error", err);
  });// Uncaught// error due to throw new Error() in setTimeout async callback// solution: return after rejectnew Promise((resolve, reject) => {
  setTimeout(() => {
    reject("error 1"); // Send reject
    console.log("Continue"); // Print

    throw new Error("error 2"); // Did run and cause Uncaught error
  }, 0);})
  .then(data => {})
  .catch(err => {
    console.log("Error", err);
  });// Caught// Only error 1 is sent// error 2 cannot be reached but can cause potential uncaught error if err = nullnew Promise((resolve, reject) => {
  setTimeout(() => {
    const err = "error 1";
    if (err) {
      reject(err); // Send reject
      console.log("Continue"); // Did print
      return;
    }
    throw new Error("error 2"); // Potential Uncaught error if err = null
  }, 0);})
  .then(data => {})
  .catch(err => {
    console.log("Error", err);
  });


查看完整回答
反對 回復 2019-08-14
  • 2 回答
  • 0 關(guān)注
  • 883 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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