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.

Dora
  • 6,776
  • 14
  • 51
  • 99
  • https://medium.com/@ExplosionPills/javascript-synchronization-patterns-ec8c05ac05be – Sully Jul 18 '19 at 18:54
  • Do `house_user.updateOrCreate(…)` and `house.addToCollection(…)` return promises that resolve properly? Your code should be working if they do. – Bergi Jul 18 '19 at 19:59

0 Answers0