I spent way too much time finding this bug. So here is my code. Apparently when i use push, the array ends up being complete, but when i use concat there is a estimated 50% chance that i will not get all concatenated items since the concats seem to run at the same time. I did not believe this to be possible, so could anyone explain to me why VERSION 1 works but version 2 does not.
let employments: any[] = [];
let companyIds = ['Id1', 'Id2']
await Promise.all(
companyIds.map( async ( companyId ) => {
const companies = await getCompaniesWithId(companyId);
// VERSION 1
employments.push( ...(await mergeWithOtherSource( companies )) );
// VERSION 2
employments = employments.concat( await mergeWithOtherSource( companies ));
} )
);
// VERSION 1 always returns 3 expected items as are in the databases
// VERSION 2 RANDOMLY returns between 1 and 3 items
return employments.length
companyIds.mapdoesn't return anything. There is nothing to await..mapis meant to map an array to another. You are just pushing to an external array which is incorrect.companyIds.mapdoes returnPromises (Promise<void>[])await mergeWithOtherSource( companies )outside ofconcat?companyIds.mapdoes return promises: jsfiddle.net/4db6ecxg/1