Best approach for Array.map on async await?

Hi, I’m a bit new to asynchronous function. Ideally, I’d like to have my users array be able to do all these moralis actions at the same time and then continue the code after all are complete.

//Is this right?
    await users.map((user) => {
        user.unset('gameId');
        user.unset('playerId');
        user.save(null, {useMasterKey:true});
    });

//or this? or something else?

    await users.map(async (user) => {
        user.unset('gameId');
        user.unset('playerId');
        await user.save(null, {useMasterKey:true});
    });

where from did you get theist of users?

now you can also make bulk operations in db: https://docs.moralis.io/moralis-server/database/bulk-queries

1 Like

users comes from a query.

Cool about the bulk update. I’ll probably put that aside until I’m running into a real need to optimize. Right now I’m dealing with less than 10 records usually.

I think I have settled on this for now.


    await Promise.all(users.map(async (user) => {
        user.unset('gameId');
        user.unset('playerId');
        await user.save(null, {useMasterKey:true});
        return null; // Edit: I might need to return something, not sure.
    }));

From here:

1 Like