gpt4 book ai didi

javascript - promise 返回前不解决

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

我正在解析一组文件并发布到数据库。作为其中的一部分,我需要保留文件中的数据总数,以及向数据库中成功插入记录的计数。 promise 不是等待所有记录都写入数据库。

我正在寻找 parserResults 方法以将 testResults 返回给调用 .reduce。它确实将其传回,但 insertSuccess = 0。

我输入了一些控制台日志以查看它在做什么,并且在递增的 insertSuccess 计数器递增之前最终结果显示在控制台上。

Console.log 结果

In parseresults ui-results-11705.json
In parseresults ui-results-14981.json
In parseresults ui-results-14982.json
In parseresults ui-results-28274.json
In parseresults ui-results-368.json

finalResult = { insertSuccess: 0,
insertFailed: 0,
testPassedCount: 2,
testFailedCount: 3 }

insertSuccess 1
insertSuccess 2
insertSuccess 3
insertSuccess 4
insertSuccess 5

这是调用将解析文件的函数的代码

 matches.reduce(function (p, val) {
return p.then(function () {
console.log('p', p);
return parser.parseResults(val);
});
}, Promise.resolve()).then(function (finalResult) {
console.log('finalResult = ', finalResult);
}, function (err) {
console.log('error in reduce', err);
});

这是被调用的方法

protractorParser.prototype.parseResults = function (fileName) {
return new Promise((resolve, reject) => {
console.log('In parseresults', fileName);
var currentFile = './testing/results/' + fileName
json.readFile(currentFile, function (err, obj) {
if (err != null) {
console.log('error reading file', err);
reject(err);
} else {
resolve(obj);
}
});
}).then(function (obj) {
var results = [];


for (var suite in obj) {
var specs = obj[suite].specs;
for (let i = 0; i < specs.length; i++) {
const assert = specs[i];
const tcR = /TC[\d]+/;
const tc = assert.description.match(tcR);

let Passed = 1;
let Message = '';
let Stack = '';
if (assert.failedExpectations.length) {
const expectation = assert.failedExpectations[assert.failedExpectations.length - 1];
Passed = 0;
Message = expectation.message;
Stack = expectation.stack.split('\n')[1].trim();
testResults.testFailedCount++
} else {
testResults.testPassedCount++
}
if (tc != null) {

const time = moment().utcOffset(config.get('settings.timeOffset')).format('YYYY-MM-DDTHH:mm:ss');
const promise = utility.TestDataManager.insertAutomationResults(tc[0], assert.description, Passed, process.env.testBuild, 'P', Message, Stack, 0, time, '');
results.push(promise.then(() => {

testResults.insertSuccess++;
console.log('insertSuccess', testResults.insertSuccess);

},
err => { console.log('… failed', err); throw err; }
));

} else {
console.log('no test case found for test: ' + assert.description + ' -- skipping');
// I don't think you want to `throw err` here, right?
}
}
}
return (Promise.all(results), testResults);
});
};

我在代码中试过几个不同的场景,但似乎无法弄清楚。任何帮助将不胜感激。谢谢克里斯汀

最佳答案

parseResults()没有正确地返回一个 promise ,因此你的 p.then().reduce()里面循环不等待任何东西。

改变这个:

return (Promise.all(results), testResults);

为此:

return Promise.all(results).then(() => testResults);

您使用的代码:return (Promise.all(results), testResults);只返回 testResults ,不是 promise 。我想你想要的是知道什么时候完成所有的 promise ,然后做出 testResults是解决的值(value)。为此,您使用 .then()Promise.all() 上然后返回 testResults来自那个.then()处理程序。这将返回一个 promise ,其解析值为 testResults .

仅供引用,您可能根本不需要这样做,因为 testResults似乎是一个更高范围的变量,您可以直接引用它。如果您愿意这样做,那么您只需更改为:

return Promise.all(results);

然后,不要使用 finalResult在你的决赛中 .then()处理程序,仅引用更高范围的 testResults直接变量。


仅供引用,一个更简洁的实现可以传入循环的每次迭代返回的对象并传递到下一次迭代,因此根本没有对更高范围变量的引用,并且一切都更加自包含。然后,您将使用我向您展示的第一种返回类型,但您将返回传入的对象,而不是更高范围的对象。

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

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