Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upsupporting foreach style for-loops #607
Comments
This comment has been minimized.
This comment has been minimized.
|
hah, this feels like such a hack but I quite like it |
This comment has been minimized.
This comment has been minimized.
|
I think it makes sense as well, using parenthesis to indicate that you want assignment and comparison is okay in other places Also, I think that the following syntax should work in newer runtimes: for (const [i, token] of tokens.entries()) {
// ...
} |
This comment has been minimized.
This comment has been minimized.
|
@yoshuawuyts it's great for async/await and generator environments because there's no new Just have to be a bit more careful about falsey values. In practice I've only run into that issue like 3 times over the last couple years though. |
This comment has been minimized.
This comment has been minimized.
jieverson
commented
Aug 31, 2016
|
What about:
|
This comment has been minimized.
This comment has been minimized.
Lets hope you're not using numbers, or strings, or anything conceivably falsy... |
This comment has been minimized.
This comment has been minimized.
|
This approach saves a function allocation and allows you to do this more easily: for (let i = 0, url; (url = urls[i]); i++) {
await fetch(url)
}@dcousens yep:
also, it's perfectly safe for other types when you have control over what you're looping over, which happens quite a bit internally. |
This comment has been minimized.
This comment has been minimized.
jieverson
commented
Sep 1, 2016
|
Still kind unreadable |
This comment has been minimized.
This comment has been minimized.
|
That's your opinion. Discussing how this code looks and the potential pitfalls of this approach is completely besides the point of this issue. Please assume that the user knows the pitfalls and doesn't share your opinion on how it looks. |
dcousens
added
the
question
label
Sep 1, 2016
This comment has been minimized.
This comment has been minimized.
|
@dcousens unreadable vs unfamiliar perhaps. I think if this were more common it could be parsed with a glance – so long as people don't hide other fancy logic in there. I've not used this pattern but it's kinda nice in that it removes the crappy extra line to assign a variable to the current iteration in a for (let i = 0; tokens.length; i++) {
const token = tokens[i] // gross
}From a quick benchmark on node 6.3.1, performance for this pattern is somewhere between
@matthewmueller If I'm not mistaken "how the code looks" and "helping the user avoid pitfalls" are some of the key concerns of |
This comment has been minimized.
This comment has been minimized.
@timoxley I don't think it looks any less readable though, in fact more readable than: for (let i = 0, len = urls.length; i < len; i++) {
let url = urls[i]
await fetch(url)
}also, afaik, you can't make a urls.forEach(url => {
// fail. needs async which makes it execute in parallel
await fetch(url)
}I don't think standard should prevent a useful pattern altogether, because there's a potential pitfall. It'll still fail if you just do that outright, but the |
This comment has been minimized.
This comment has been minimized.
|
@timoxley to be clear, I never commented on the readability, just the fact it is less safe (as it will early-exit on any falsy elements) and potentially not going to be part of "helping the user avoid pitfalls". |
This comment has been minimized.
This comment has been minimized.
@dcousens whoops somehow I read this as your comment, my bad. |
This comment has been minimized.
This comment has been minimized.
|
I think it's fine to allow this behavior as an opt-in with explicit parens. We already allow this type of opt-in in other places in |
This comment has been minimized.
This comment has been minimized.
|
@matthewmueller Are you using the latest version of This code lints without any issues for me. var tokens = []
for (let i = 0, token; (token = tokens[i]); i++) {
console.log(i, token)
}Apparently this used to be a bug in an old version of eslint, but it was fixed a while ago. Your issue should be fixed by upgrading |
feross
closed this
Sep 10, 2016
This comment has been minimized.
This comment has been minimized.
|
interestinggg. yep upgrading seems to fix it. sorry for the false alarm! |
matthewmueller commentedAug 30, 2016
•
edited
One pattern I use a lot is a for-loop that looks like this:
This errors out, saying:
I think this is totally appropriate. Unfortunately there's no way to get around this error without changing the loop completely. Here's a syntax that I think should work:
Since it's explicit that there's an assignment, but it'll give the same error as previously.