2016-02-27 89 views
0

我在工作者中擁有這段代碼。這兩個承諾/塊的catch語句都沒有執行

var cBtnStore = { 
    a: function(){} 
}; 

現在,公告不存在b關鍵在那裏。

然後我在我的工作人員有一個功能。它這樣做是:

function run() { 
    ... 
    self.postMessageWithCallback(function() { 
     cBtnStore.b(); ////// there is no b key, this should prevent going to the return 
    }); 
    ... 
    return promise; // returns a promise 
} 

var gCB = {}; 
self.postMessageWithCallback = function(aMsgArr, aCB) { 
     var gCBID++; 
     aMsgArr.push(gCBID); 
     gCB[gCBID] = aCB; 
     self.postMessage(aMsgArr); 
} 

self.onmessage = function(msg) { 
    var aCBID = msg.data.pop(); 
    gCB[aCBID].apply(msg.data); 
}; 

然後我做到這一點的工人:

try { 
    run().then(x=>{}, y=>{}).catch(z=>{console.error('promise caught')}); 
} catch(err) { 
    console.log('runComplete threw'); 
} 
console.log('result:', result); 

實際發生的 - 這將記錄到控制檯的runComplete()返回值是"result:" [Promise object]。沒有執行catch.catch語句。

我所期待的應該發生 - 它應該觸發了承諾或try-catch塊的catch聲明。

我的問題 - 是否有反正做這個捕獲?

+1

你向我們展示'run()'的聲明,但是你用另一個名字'runComplete()'調用一個函數。另外,'result'在哪裏聲明並賦值? – nnnnnn

+0

這有點複雜,這個代碼是在一個工人。我以簡化的方式添加了更多的代碼。爲了簡化它,我犯了這個錯字。我修好了它。 @nnnnnn – Noitidart

+1

你的處理器在'.then'的第二個參數中處理錯誤。 –

回答

1

當你寫

run().then(x=>{}, y=>{}).catch(z=>{console.error('promise caught')}); 

你的論點y=>{}實際上是指 「吃了所有的錯誤」。如果你想看到錯誤,你應該寫

run().then(x=>{}).catch(z=>{console.error('promise caught')}); 
+0

不行!在承諾這麼長時間後,我無法相信我沒有意識到這一點!這固定它謝謝! – Noitidart