gpt4 book ai didi

javascript - promise 条件相等

转载 作者:行者123 更新时间:2023-12-02 22:17:16 25 4
gpt4 key购买 nike

我想在代码中等待,直到两个值相同。为此我使用

    await new Promise((resolve, reject) => {
if(curCount == maxTests) resolve;
});

但我认为,这仅被调用一次。如果两个值相同,我怎样才能解决 promise ?如何避免它永远不会发送解析?

更新:有些要求的功能会带来麻烦。这是整个函数,没有子函数。该函数将填充 q 队列以完成测试同步。问题是 req.body.selection.forEach 立即返回,但我想等到整个队列准备好。所以我的想法是在最后添加一个 promise ,直到当前和最大值相同。

router.post('/imgtest', async (req, res) => {

console.log('Start Image Test');

//Er kommt an.
req.setTimeout(5000000); // Nach Adam Riese 83 Minuten.
process.setMaxListeners(0);

io = req.app.get('socketio');

//Caluclate the max amounnt of tests
const maxTests = req.body.selection.length * req.body.servers.length;
var curCount = 0;


//RETURN IF THIS IS READY. CURRENTLY IT RETURNS IMMEDIATLY
req.body.selection.forEach(async function(entry) {

//Jetzt erstmal die Domain aus der DB holen
var dbUrl = await getUrl(entry);
console.log('tapsi');

var bildFormat = '';

var arrQuestionmark = dbUrl.split('?');
if(arrQuestionmark.length==2){
if(arrQuestionmark[1].includes('&')){
var arrAnd = arrQuestionmark[1].split('&');
arrAnd.forEach(function(entry) {
if(entry.includes('format=')){
var arrFormat = entry.split('=');
bildFormat = arrFormat[1];

}
});
}
}

var masterName = uuidv1();
const orgpath = path.resolve(__basedir, 'tests/downloads', masterName + '-.' + bildFormat);

//Download the MAsterimage
(async () => {

await queue.add(() =>downloadImage(dbUrl, 'c11', req.body.domain, bildFormat, orgpath) );

})();

req.body.servers.forEach(async function(xserver) {

var fileName = masterName + '-' + xserver + '.' + bildFormat;

const dpath = path.resolve(__basedir, 'tests/downloads', fileName);

(async () => {
await queue.add(() => downloadImage(dbUrl, xserver, req.body.domain, bildFormat, dpath));
//console.log('Done ' + entry);
})();

(async () => {
await queue.add(async() => startCompare(orgpath, dpath, 'null:').then(function(result) {
console.log(result);
curCount++;
messageIO(curCount,maxTests);
}));
//console.log('done compare ' + entry);
//fs.unlinkSync(dpath);
})();



});


});


console.log('Need to wait');

res.sendStatus(200);

});

最佳答案

您假设只会被调用一次是正确的。解决这个问题的方法是在函数内通过 setInterval 创建一个循环 - 进行定期检查并解析是否为 true 并清除循环。

关于javascript - promise 条件相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59345251/

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