I have a function that receives an array of usernames. For each one I need to get the connections IDs and concatenate into another array. But I think the way I did have some problems of race condition that may colide and concatenate less results that it should...
const getConnections = async function (usernames) {
let connections = [];
await Promise.all(usernames.map(async (username) => {
try {
let connsFound = await dynamo.getConnectionsByUsername(username);
if (connsFound && connsFound.length > 0)
connections = connections.concat(connsFound);
} catch (error) {
console.error('ERROR GET CONNECTIONS', error)
}
}));
return connections;
};
the result of the connections its an array, so the result I would like to merge to the var connections...but .concat does not merge, it creates a new array, so I need to do a 'var = var.concat(newArray)'
I am affraid this is not safe and in some operation it will colide and overwrite some results...
Is there a better way to do it?
cheers
return connsFound;instead, and uselet connections = await Promise.all(...(Promise.all will resolve to an array of the Promise results, in the same order, which is exactly what you want in the first place, there's no need to concat anything here)