[英]bluebird promise catch() is not calling inner function with Promise.CancellationError
[英]Bluebird promise is not calling .then intermittently
我正在學習bluebird,並在我的代碼中實現如下:
var getPromiseA = new Promise(function (resolve, reject) {
executeCommand('command A')
.then(function (result) {
var temp = JSON.parse(result);
for (var i in temp) {
if (temp[i].abc === 'ABC') {
resolve(temp[i]);
}
}
}).catch(function (err) {
console.log('Error occurred while executing getPromiseA :::');
reject(err);
});
});
var getPromiseB = new Promise(function (resolve, reject) {
executeCommand('command B')
.then(function (result) {
resolve(JSON.parse(result));
})
.catch(function (err) {
console.log('Error occurred while executing getPromiseB :::');
reject(err);
});
});
var getPromiseC = new Promise(function (resolve, reject) {
executeCommand('command C')
.then(function (result) {
var temp = JSON.parse(result);
for (var i in temp) {
console.log('in command C::::');
if (temp[i].abc != null) {
resolve(temp[i]);
}
}
})
.catch(function (err) {
console.log('Error occurred while executing getPromiseC :::');
reject(err);
});
});
function executeCommand(inputCmd) {
var commandPromise = new Promise(function (resolve, reject) {
var response = {}, err = {};
var command = exec(inputCmd);
command.stdout.on('data', function (data) {
response = data;
});
command.stderr.on('data', function (data) {
err = data;
});
command.on('close', function (code) {
console.log('coming in close' + code);
if (code === 0) {
console.log('before resolving in executeCommand');
resolve(response);
console.log('after resolving in executeCommand' + JSON.stringify(response));
}
else {
reject(code);
}
});
});
return commandPromise;
}
在此之后,我將使用Promise.all函數來解決所有承諾。 解決getPromiseC時遇到問題。 PromiseC不會進入.then。 我不知道是什么原因。
輸出如下所示:對於getPromiseA-1.解析為executeCommand之前2.解析為executeCommand-{data}之后3.getPromiseA .then
對於getPromiseB-1.在執行executeCommand之前2.在執行executeCommand之后-{data} 3.getPromiseB .then
對於getPromiseC-1.解析為executeCommand之前2.解析為executeCommand之后-{data}
不打印'getPromiseC .then'控件卡在這里。但是當再次嘗試執行該功能時,它解決了所有的諾言,即,我可以看到getPromiseC。然后正在控制台中打印。
由於此問題是間歇性發生的,因此我很困惑。重新加載解決了該問題。 當我多次調用該函數時,我看到此解決問題是隨機發生的。
有人可以幫我學習這段代碼有什么問題嗎?
您應該避免Promise
構造函數antipattern :
var promiseA = executeCommand('command A').then(function (result) {
var temp = JSON.parse(result);
for (var i in temp) {
if (temp[i].abc === 'ABC') {
return temp[i];
// ^^^^^^
}
}
}).catch(function (err) {
console.log('Error occurred while executing getPromiseA :::');
throw err;
});
var promiseB = executeCommand('command B').then(JSON.parse).catch(function (err) {
console.log('Error occurred while executing getPromiseB :::');
throw err;
});
var promiseC = executeCommand('command C').then(function (result) {
var temp = JSON.parse(result);
for (var i in temp) {
console.log('in command C::::');
if (temp[i].abc != null) {
return temp[i];
// ^^^^^^
}
}
}).catch(function (err) {
console.log('Error occurred while executing getPromiseC :::');
throw err;
});
這樣一來,您還將消除錯誤的根源:當在temp
找不到合適的值時,承諾將永遠無法解決。 現在,您的諾言將僅使用回調函數隱式返回的undefined
值來解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.