Reducing Over Unresolved Promises
Summing async results with reduce forgets to await the Promises, causing string coercion instead of arithmetic.
Codejavascript
async function fetchScore(id) {
return id * 10;
}
const ids = [1, 2, 3];
function totalScore(ids) {
const total = ids
.map((id) => fetchScore(id))
.reduce((acc, score) => acc + score, 0);
return total;
}
console.log(totalScore(ids));What is the bug in this code, and what does totalScore actually return?