-
Notifications
You must be signed in to change notification settings - Fork 61
docs: describe Array#forEach anti patterns #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
async function doSomething(x) { await ... }
// Probably doesn't do what you expect.
items.forEach(item => { await doSomething(item })
// vs
// Blocks the loop as expected.
for (const item of items) {
await doSomething(item)
} |
This is a syntax error: items.forEach(item => { await doSomething(item) }) This will items.forEach(async item => { await doSomething(item) }) This is how you'd have to refactor this to be async: await Promise.all(items.map(async item => { await doSomething(item) })) I'll add this as an example in the Flexibility heading. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow thanks for taking the time to document this. Looks good. 👍
Closes #86.
This describes in more detail why we discourage
forEach
, instead preferringfor...of
./cc @koddsson @zeke