JavaScript async/await warnings
TLDR; NEVER EVER EVER USE map, filter, reduce
with async/await
Frustrated over why a segment of code was not working which looked like this
Example
const results = inputs.map(async z => {
return {
id: getId(z),
result: await sendId(z)
}
});
The structure and formatting is correct and you would think this would work I have marked the mappable input to be async and am awaiting the result, but no it would immediately return when running this. Spent a few hours thinking over why this was happening, but then discovered that map, filter, reduce
never honor the async/await operators and return immediately. In this case you should use a for loop as it will honor the await operations properly. That means the code should look like this
const results = [];
for (let z of inputs){
const awaitedResult = {
id: getId(z),
result: await sendId(z)
};
results.push(awaitedResult);
}