javascript - promise 条件相等

标签 javascript node.js promise

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

    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/

相关文章:

JavaScript - 无法正确访问对象内的属性

javascript - @nestjs/schematics 的意义是什么?即使创建了 NestJs 应用程序后,我们还需要该包吗?

javascript - 我什么时候应该使用 Q.defer 什么时候只使用 Promise.resolve/reject?

javascript - Mongoose 顺序 promise

node.js - Firebase 在 promise 触发之前等待加载数据库数据

javascript - 为什么我不能在 javascript 中将 1 加到一个大数上

javascript - Protractor:如何使用 href 子字符串或 span 子字符串单击元素?

javascript - 使用 Mozilla SDK 的 Firefox 扩展中带有面板的切换按钮

javascript - 如何在 Javascript 中修改 URL

javascript - 如何同步运行嵌套的异步方法?