gpt4 book ai didi

javascript - promise 在返回前不解决

转载 作者:行者123 更新时间:2023-11-30 14:44:47 25 4
gpt4 key购买 nike

我很难弄清楚 promise 是如何工作的。我有以下代码,我正在执行遍历数组。

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() 处理程序中运行另一个异步操作时,您必须从 返回该 promise >.then() 以将其正确链接到主 promise 链中。如果您不这样做,那么父级 promise 根本不会与该内部 promise 相关联(它成为自己独立的 promise 链,您无法监控它)并且父级 promise 不会等待内部 promise 。

以下是所做的更改:

  1. 移除在另一个 promise 中不必要地包装一个 promise 的 promise 反模式(只返回你已经拥有的 promise)
  2. 从 .then() 处理程序中返回嵌套的 promise 以适本地链接它们
  3. 将错误记录合并到一个地方并重新抛出错误,以便正确返回
  4. 删除 Promise.all()
  5. 删除 results.push(resultsArray); 因为这似乎没有任何意义。按照我在下面编写代码的方式,顶级 promise 解析为 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;
});
}

关于javascript - promise 在返回前不解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49122578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com