0

I have read posts such as Using async/await inside for loop

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

I am trying to run a loop which saves some data into the db sequentially

somehow I am not getting it to work and it's still running in parallel.

I have tried both of these by using for of loop like such

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)
}

using 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)
}

both ways still runs in parallel though.

Thanks in advance for any suggestions and help.

2
  • medium.com/@ExplosionPills/… Commented Jul 18, 2019 at 18:54
  • Do house_user.updateOrCreate(…) and house.addToCollection(…) return promises that resolve properly? Your code should be working if they do. Commented Jul 18, 2019 at 19:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.