[英]Promise not resolving before returning
我很難弄清楚諾言如何運作。 我有以下代碼,正在通過數組執行迭代。
runOpts.automationResults.reduce(function (p, val) { return p.then(function () { return pd.run(val); }); }, Promise.resolve()).then(function (rallyResults) { console.log('returned from method'); console.log('rallyResults ', rallyResults); }, function (err) { console.log('error in reduce', err); });
它正在調用以下方法
ProcessDatabase.prototype.run = function (options) { return new Promise((resolve, reject) => { var dal = new db(); var resultsArray = { results: [], options: [], testSet: [], testIteration: '', multipleSets: 0, totalCount: 0 } resultsArray.options = options; dal.getAutomationResultsByBuild(options.Build).then((results) => { resultsArray.results = results; resultsArray.totalCount = results.data.length; resolve(resultsArray); }) }).then(function (resultsArray) { var results = []; //console.log('resultsArray', resultsArray); //console.log(`Starting Rally publishing for Build '${resultsArray.options.Build}'...`) if (resultsArray.options.Executiontype == 'ci') { rallyApi.autoDiscoverTestSets().then((sets) => { resultsArray.testSet = sets.sets; resultsArray.testIteration = sets.iteration; resultsArray.multipleSets = sets.sets.length > 1; results.push(resultsArray); console.log('results', results); }, (err) => { reject(err); }); // totalResults = totalResults + results.data.length; } else { rallyApi.addTestSet('Automation: ' + resultsArray.options.type + ' - ' + resultsArray.options.build, config.get('rally.projects.testing.ref'), null, resultsArray.options.SessionUser).then((resp) => { //console.log(resp); console.log('New test set ' + resp.FormattedID + ' created.'); resultsArray.multipleSets = 0; resultsArray.testSet = resp.FormattedID; //console.log('resultsArray', resultsArray); results.push(resultsArray); console.log('results', results); }, (err) => { console.log(err); }); } console.log('results', results); return Promise.all(results); }) }
我已經嘗試了幾種不同的迭代方式,並且總是在ProcessDatabase.prototype.run方法中完成結果之前返回到調用.reduce。
我已經輸入了控制台日志,這就是我得到的。 我可以看到最終結果數組具有我需要的所有信息。 我只是無法弄清楚如何將其傳遞回調用.reduce。
---These are actually from the .reduce and are written to the log before the results in the method called returned from method rallyResults [] **----These are from the ProcessDatabase.prototype.run method called** **This is the contents of the "results" set the first iteration through** -- Found 2 test sets. results [ { results: { _success: true, _message: '', _data: [Array] }, options: { Executiontype: 'ci', Build: 'test_030518_103956', Environment: 'dev', SessionUser: 'https://rally1.rallydev.com/slm/webservice/v2.0/user/165547093296' }, testSet: [ [Object], [Object] ], testIteration: 'https://rally1.rallydev.com/slm/webservice/v2.0/iteration/184203152680', multipleSets: true, totalCount: 4 } ] New test set TS2969 created. **This is the contents of the "result" set after the second iteration through and what I trying to get passed back to the calling .reduce.** results [ { results: { _success: true, _message: '', _data: [Array] }, options: { Executiontype: 'ci', Build: 'test_030518_103956', Environment: 'dev', SessionUser: 'https://rally1.rallydev.com/slm/webservice/v2.0/user/165547093296' }, testSet: [ [Object], [Object] ], testIteration: 'https://rally1.rallydev.com/slm/webservice/v2.0/iteration/184203152680', multipleSets: true, totalCount: 4 }, { results: { _success: true, _message: '', _data: [Array] }, options: { Executiontype: 'regression', Build: 'test_030518_110447', Environment: 'dev', SessionUser: 'https://rally1.rallydev.com/slm/webservice/v2.0/user/165547093296' }, testSet: 'TS2969', testIteration: '', multipleSets: 0, totalCount: 6 } ]
任何幫助將不勝感激。 謝謝克里斯汀
這段代碼中有很多問題,但是主要的問題是,當您在.then()
處理程序中運行另一個異步操作時,您必須從.then()
返回該諾言才能正確地將其鏈接到主程序中。承諾鏈。 如果您不這樣做,那么父承諾根本就不會鏈接到該內部承諾(它變成了自己的獨立承諾鏈,您無法對其進行監視),並且父承諾不會等待內部承諾。
以下是所做的更改:
Promise.all()
。 results.push(resultsArray);
因為似乎沒有任何意義。 我在下面編寫代碼的方式是,最高級別的承諾將解析為resultsArray
並且由於其中只有一個,因此我認為無需將其嵌入另一個數組中。 這是修改后的代碼:
ProcessDatabase.prototype.run = function (options) {
var dal = new db();
var resultsArray = {
results: [],
options: {},
testSet: [],
testIteration: '',
multipleSets: 0,
totalCount: 0
}
resultsArray.options = options;
return dal.getAutomationResultsByBuild(options.Build).then((results) => {
resultsArray.results = results;
resultsArray.totalCount = results.data.length;
return resultsArray;
}).then(function (resultsArray) {
//console.log('resultsArray', resultsArray);
//console.log(`Starting Rally publishing for Build '${resultsArray.options.Build}'...`)
if (resultsArray.options.Executiontype == 'ci') {
return rallyApi.autoDiscoverTestSets().then((sets) => {
resultsArray.testSet = sets.sets;
resultsArray.testIteration = sets.iteration;
resultsArray.multipleSets = sets.sets.length > 1;
return resultsArray;
});
} else {
return rallyApi.addTestSet('Automation: ' + resultsArray.options.type + ' - ' + resultsArray.options.build, config.get('rally.projects.testing.ref'), null, resultsArray.options.SessionUser).then((resp) => {
//console.log(resp);
console.log('New test set ' + resp.FormattedID + ' created.');
resultsArray.multipleSets = 0;
resultsArray.testSet = resp.FormattedID;
return resultsArray;
});
}
}).catch(err => {
// log error and rethrow
console.log(err);
throw err;
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.