问题
I have come JS code with some infinite loops (we're using node-fibers to allow things to happen).
ESLint hates:
while (true) {
}
because of the constant condition.
The following is allowed though:
for(;;) {
}
Beyond just feeding the lintbeast, are there any objective reasons to favour for
over while
(or vice versa)?
NOTE: This question is explicitly requesting objective reasons and so is not simply opinion based.
回答1:
These rules about infinite loops are from before generators were a thing and are not even aware of fibers.
Under the assumption that every function never suspends and returns (like a generator, async
-keyword function or a fiber) the rule makes a lot of sense to warn against constants in loops.
Now that times have changed - the rule no longer makes sense and what you're doing is perfectly fine.
If we check the eslint repo it was discussed and deemed "not important enough to acknowledge" in the meantime:
I don't think this makes sense as a built-in exception. If you're doing that, then it's best to manually disable the rule in the generator using a comment.
The workaround for(;;)
was suggested but everyone involved understands it's a hack for this particular case.
Disable the rule.
来源:https://stackoverflow.com/questions/37466508/why-does-eslint-trigger-lint-errors-on-whiletrue-using-fibers