【发布时间】:2019-12-15 15:20:13
【问题描述】:
我想在我的代码中等待,直到两个值相同。为此,我使用
await new Promise((resolve, reject) => {
if(curCount == maxTests) resolve;
});
但我认为,这只会被调用一次。如果两个值相同,我如何才能使承诺得到解决?如何避免它永远不会发送解析?
更新:
有的请求了麻烦的功能。这是整个功能,没有它的子功能。该函数将填充 q-queue 以填充测试同步。问题在于req.body.selection.forEach 立即返回,但我想等到整个队列准备好。所以我的想法是在结尾添加一个承诺,直到当前和最大值相同。
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);
});
【问题讨论】:
-
好像an XY problem。您能否分享有关您要解决的问题的更多详细信息?现在这段代码并没有真正告诉我们确切的问题是什么。
-
您所拥有的不会等到值相等。你是对的,它只会运行一次。您能否分享更多关于您要完成的工作的信息,或者更多代码?
-
您能否发布代码来说明您如何修改 curCount 和 maxTests?
-
我已经添加了请求的信息。希望这能更好地解释我的问题。
-
您应该阅读一些好书或教程来解释
await/async的作用。像(async () => { await queue.add ( … ); })();这样的代码没有任何意义。您无需等待完成即可调用匿名async函数。在其中,您有一个await,它对您的代码没有任何影响。这相当于只写queue.add ( … );。而req.body.selection.forEach(async function(entry) {也是没有任何意义的东西。使用for … of循环,或使用.map(async function(entry) {和Promise.all。
标签: javascript node.js promise