JSのforEach内部でawaitが使えない

概要

詳細

対処法

理由

let ratings = [5, 4, 5];
let sum = 0;
let sumFunction = async function (a, b) {
  return a + b;
};

ratings.forEach(async function (rating) {
  sum = await sumFunction(sum, rating);
});

console.log(sum);
// Naively expected output: 14
// Actual output: 0