OneBug This week's bugs
Spot the bug JavaScript
1
// Sum the stock across every warehouse, skipping any blanks.
2
async function totalStock(ids) {
3
  let total = 0;
4
  const active = ids.filter((id) => id != null);
5
  active.forEach(async (id) => {
6
    total += await fetchQty(id);
7
  });
8
  return total;
9
}
10
 
11
// fetchQty("a") -> 2, "b" -> 5, "c" -> 1
12
totalStock(["a", "b", "c"]).then((t) => console.log(t));
13
// expected: 8
14
// actual:   0  (!)

Read the full write-up for this bug (spoilers)