這可能是由於幾個原因,無法確定哪些來自帖子。因此,爲了幫助未來的讀者 - 讓我們弄清楚什麼可以去錯在上面的場景:
讓我們做出如下調整到代碼:
function a() {
console.log("a called"); // if this doesn't log, you're redefining 'a'
return Promise.all([promise1, promise2]).then((results) => {
console.log("In .all"); // if this isn't called, promise1 or promise2 never resolve
return promise3(results);
}).tap(() =>
console.log("Promise 3 fn resolved") // if this doesn't log, promise3 never resolves
});
}
然後,我們then
處理程序:
let r = a().then((result) => {
console.log('result', result); // this logs together with promise 3 fn
}); // bluebird automatically adds a `.catch` handler and emits unhandledRejection
setTimeout(() => {
// see what `r` is after 30 seconds, if it's pending the operation is stuck
console.log("r is", r.inspect());
}, 30000);
這應該會給你全面覆蓋所有可能出錯的情況。
如果我必須猜測 - 其中一個承諾永遠不會解決,因爲您有一個Promise
構造函數,因此永遠不會調用resolve
或reject
。
(藍鳥會警告你對這個在某些情況下 - 請確保您有警告)
另一種情況可能導致這抵消 - 但我假設你沒有這樣的開啓。
快樂編碼。
什麼是'promise3'? – Boaz
@Boaz它是一個函數,返回一個數據庫查詢的承諾。當我添加'.then'時,它顯示查詢結果(我的意思是'promise3'運行良好) – Hadi