Skip to content

Commit

Permalink
Merge pull request #149 from github/fix-array-foreach-docs
Browse files Browse the repository at this point in the history
Update array-foreach.md
  • Loading branch information
srt32 committed Dec 8, 2021
2 parents 4dddd61 + 05be242 commit 9393b2f
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions docs/rules/array-foreach.md
Expand Up @@ -58,13 +58,11 @@ for (const apple of apples) {
}
```

If `polishApple` need to do some async work, then we'd need to refactor the iteration steps to accomodate for this async work, by `await`ing each call to `polishApple`. We cannot simply pass an `async` function to `forEach`, as it does not understand async functions, instead we'd have to turn the `forEach` into a `map` and combine that with a `Promise.all`. For example:
If `polishApple` needed to do some serial async work, then we'd need to refactor the iteration steps to accomodate for this async work, by `await`ing each call to `polishApple`. We cannot simply pass an `async` function to `forEach`, as it does not understand async functions, instead we'd have to turn the `forEach` into a `reduce` and combine that with a `Promise` returning function. For example:

```diff
- apples.forEach(polishApple)
+ await Promise.all(
+ apples.map(polishApple)
+ )
+ await apples.reduce((cur, next) => cur.then(() => polishApple(next)), Promise.resolve())
```

Compare this to the `for` loop, which has a much simpler path to refactoring:
Expand Down

0 comments on commit 9393b2f

Please sign in to comment.