for循环以及具有异步和等待顺序的for循环

时间:2019-07-18 18:53:04

标签: javascript for-loop async-await try-catch for-of-loop

我阅读了诸如 Using async/await inside for loop

Unexpected `await` inside a loop. (no-await-in-loop)

我正在尝试运行一个循环,该循环将一些数据保存到数据库sequentially

不知何故我无法使用它,并且它仍在parallel中运行。

我已经通过使用for of loop这样的方式尝试过这两种方式

for (const [index, user] of users.entries()) {
    const response = await house_user.updateOrCreate(
        { employee_id: user.employee_id },
        {
            employee_id: user.employee_id,
            phone: user.phone,
            full_name: user.full_name,
            email: user.email,
        }
    );
    await house.addToCollection(rs.id, 'users', response.id)
}

使用for loop

for (let i = 0; i < users.length; i++) {
    const response = await house_agent.updateOrCreate(
        { employee_id: users[i].employee_id },
        {
            employee_id: users[i].employee_id,
            phone: users[i].phone,
            full_name: users[i].full_name,
            email: users[i].email,
        }
    );
    await house.addToCollection(rs.id, 'users', response.id)
}

这两种方式仍然并行运行。

在此先感谢您的建议和帮助。

1 个答案:

答案 0 :(得分:-1)

尝试一下:

            const updateDB = user => {
                return new Promise((resolve, reject) => {
                     house_agent.updateOrCreate(
                        { employee_id: user.employee_id },
                        {
                            employee_id: user.employee_id,
                            phone: user.phone,
                            full_name: user.full_name,
                            email: user.email,
                        }
                    ).then(data => {
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    });
                });
            };

            for (let i = 0; i < users.length; i++) {
                updateDB(users[i]).then(response => {
                    await house.addToCollection(rs.id, 'users', response.id)
                }).catch(err => {
                    // Do what you have to do
                })
            }

如果它不起作用,请尝试以下操作:

            const updateDB = user => {
                return new Promise(async (resolve, reject) => {
                     const response = await house_agent.updateOrCreate(
                        { employee_id: user.employee_id },
                        {
                            employee_id: user.employee_id,
                            phone: user.phone,
                            full_name: user.full_name,
                            email: user.email,
                        }
                    );
                    resolve(response)
                });
            };

            for (let i = 0; i < users.length; i++) {
                updateDB(users[i]).then(response => {
                    await house.addToCollection(rs.id, 'users', response.id)
                }).catch(err => {
                    // Do what you have to do
                })
            }