Skip to content

Commit 9393b2f

Browse files
authored
Merge pull request #149 from github/fix-array-foreach-docs
Update array-foreach.md
2 parents 4dddd61 + 05be242 commit 9393b2f

1 file changed

Lines changed: 2 additions & 4 deletions

File tree

docs/rules/array-foreach.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,11 @@ for (const apple of apples) {
5858
}
5959
```
6060

61-
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:
61+
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:
6262

6363
```diff
6464
- apples.forEach(polishApple)
65-
+ await Promise.all(
66-
+ apples.map(polishApple)
67-
+ )
65+
+ await apples.reduce((cur, next) => cur.then(() => polishApple(next)), Promise.resolve())
6866
```
6967

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

0 commit comments

Comments
 (0)