不应该等待强制执行等待吗? [英] Shouldn't await force execution to wait?

查看:59
本文介绍了不应该等待强制执行等待吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下所示的JavaScript代码(在Node中使用Sequelize):

I have JavaScript code that looks like this (using Sequelize in Node):

if (require.main == module) {
    (async () => {
        const my_var = await MyModel.createWithChildren('name', ['one', 'two']);
        console.log('before');
        console.log(await my_var.getChildren());
        console.log('after');
    })();
}

MyModel 具有如下所示的静态方法:

MyModel has a static method that looks like this:

static async createWithChildren(name, children) {
    const me = await Me.create({name: name});
    const child_recs = await Child.bulkCreate(children.map((child) => ({child: child})));
    child_recs.forEach(async (rec) => {
        await rec.setParent(me);
        await rec.save();
    });
    return me;
}

但是,当我执行此代码时,每次对Sequelize API的调用都以 await 开头,我得到了"after"消息.在最后一个子项更新之前记录日志:

But when I execute this code, where every call to the Sequelize API is preceded by an await, I get the "after" log before the last child update:

before
Executing (default): SELECT `id` ... FROM `children` WHERE `parent_id` = 1;
Executing (default): UPDATE `children` SET `parent_id`=? ...
[]
after
Executing (default): UPDATE `children` SET `parent_id`=? ...

我认为 SELECT 来自调用 my_var.getChildren(),但在"before,"之前记录.空列表是因为它没有等待"消息.为 my_var.getChildren()调用?如果一切都在按我的期望进行,日志将如下所示:

The SELECT is coming from, I think, the call the my_var.getChildren() but is logging after "before," and the empty list is because it didn't "await" for the my_var.getChildren() call? If everything were awaiting as I expect, the log would look something like this:

before
Executing (default): UPDATE `children` SET `parent_id`=? ...
Executing (default): UPDATE `children` SET `parent_id`=? ...
Executing (default): SELECT `id` ... FROM `children` WHERE `parent_id` = 1;
['one', 'two']
after

因此,如果我正在等待"每个电话,为什么我看不到?

So if I'm "awaiting" every call, why am I not seeing that?

推荐答案

正如其他人指出的那样,关键因素是 async 函数将 forEach 的结果功能未正确等待.

As others have pointed out, the key factor is that the async function the results of your forEach function are not being properly awaited.

要做的第一件事是将 forEach 切换到 map .这样就可以使操作现在返回一个promise数组.对于调试,将其值放入变量 promises .

The first thing to do is to switch your forEach to a map. That will make it so that operation now returns an array of promises. For debugging, it can be helpful to put that value into a variable promises.

到那时,您可能会看到需要在您的 createWithChildren 函数中等待这些承诺.

At that point you may see that those promises need to be awaited within you createWithChildren function.

static async createWithChildren(name, children) {
    const me = await Me.create({name: name});
    const child_recs = await Child.bulkCreate(children.map((child) => ({child: child})));
    const promises = child_recs.map(async (rec) => {
        await rec.setParent(me);
        await rec.save();
    })
    await Promise.all(promises);
    return me;
}

这篇关于不应该等待强制执行等待吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆