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
聲明。
我的問題 - 是否有反正做這個捕獲?
你向我們展示'run()'的聲明,但是你用另一個名字'runComplete()'調用一個函數。另外,'result'在哪裏聲明並賦值? – nnnnnn
這有點複雜,這個代碼是在一個工人。我以簡化的方式添加了更多的代碼。爲了簡化它,我犯了這個錯字。我修好了它。 @nnnnnn – Noitidart
你的處理器在'.then'的第二個參數中處理錯誤。 –