After spending an excessive amount of time tracking down a bug, I am sharing my code with you all. Interestingly enough, I discovered that when using the push method, the array is complete. However, when using concat, there is a 50% chance that not all concatenated items will be received, as the concats appear to run simultaneously. I initially thought this was impossible. Can someone please explain 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 consistently returns 3 expected items as found in the databases
// VERSION 2 RANDOMLY returns between 1 and 3 items
return employments.length