1

According to ESLint some code like this is not 'clean code'

for(;;) {
  await *async function*
}

My aim is to infinitely loop a certain function, executing it one by one, without eventually crashing my app because of call stack limits. I have thought about it for some time but couldn't come up with anything else that would do the same. What ESLint suggests also wouldn't work in my case; they suggest starting all of the functions in the loop and awaiting their resolve/reject callback outside of the loop by using .all().

Help would be appreciated! I just want to write this as cleanly as possible

timgfx
  • 199
  • 2
  • 14
  • 2
    how about `while(true)`? – Chris Riebschlager Jan 21 '18 at 18:41
  • @ChrisRiebschlager ESLint doesn't like await in any loop https://eslint.org/docs/rules/no-await-in-loop It might just be the only solution for what I want to do, so then it wouldn't be a problem, but otherwise id of course like the cleanest solution :P – timgfx Jan 21 '18 at 18:42
  • 2
    Why is what ESLint logs important? – guest271314 Jan 21 '18 at 18:44
  • Ok! How about `setInterval`? – Chris Riebschlager Jan 21 '18 at 18:44
  • @ChrisRiebschlager that wouldn't wait for the function to be completed, it would be a guess at best :P But, like estus said Ill just remove the rule. I started using eslint today and really like it so I guess it kinda made me go crazy x) – timgfx Jan 21 '18 at 18:47

2 Answers2

7

As the ESLint documentation says:

In many cases the iterations of a loop are not actually independent of each-other. For example, the output of one iteration might be used as the input to another. Or, loops may be used to retry asynchronous operations that were unsuccessful. In such cases it makes sense to use await within a loop and it is recommended to disable the rule via a standard ESLint disable comment.

So if it makes sense for you to wait in every iteration, disable this rule. If you can parallelize the async calls use Promise.all.

To disable an ESLint rule only at some place in the code do it like this:

/* eslint-disable no-await-in-loop */
//Your code here...
/* eslint-enable no-await-in-loop */
mdatsev
  • 3,054
  • 13
  • 28
-1

Yes, there are several patterns which could be used to "infinitely loop". You can schedule the same function to be called when the function completes.

guest271314
  • 1
  • 15
  • 104
  • 177
  • This would eventually crash my app because of the call stack ;( – timgfx Jan 21 '18 at 18:44
  • No, it should not "crash" the app. – guest271314 Jan 21 '18 at 18:44
  • Create a function that calls itself, run your code and wait for a bit. Eventually youll get that call stack error – timgfx Jan 21 '18 at 18:45
  • That is not accurate. See [multiple, sequential fetch() Promise](https://stackoverflow.com/questions/38034574/multiple-sequential-fetch-promise/). You can also use an async iterator to perform the same task, see the code at this [Answer](https://stackoverflow.com/a/48349837) at [Run multiple recursive Promises and break when requested](https://stackoverflow.com/questions/48349721/run-multiple-recursive-promises-and-break-when-requested) – guest271314 Jan 21 '18 at 18:46
  • @timgfx Have already tried the code several times over. If your concern is what ESLint logs do not use ESLint – guest271314 Jan 21 '18 at 18:49
  • function justTryItAndYoullSee() { justTryItAndYoullSee(); } justTryItAndYoullSee(); – timgfx Jan 21 '18 at 18:51
  • @timgfx you should read about [tail calls](http://benignbemine.github.io/2015/07/19/es6-tail-calls/) – Sidney Jan 21 '18 at 18:52
  • [How can I make my slider loop continuously](https://stackoverflow.com/questions/43595870/how-can-i-make-my-slider-loop-continuously/), [Does it consider recursion if I call onload event on the funcion which I'm inside?](https://stackoverflow.com/questions/45329784/does-it-consider-recursion-if-i-call-onload-event-on-the-funcion-which-im-insid) – guest271314 Jan 21 '18 at 18:53
  • @Sidney since my example has to be infinite that wont work since itll still exceed the call stack at some point – timgfx Jan 21 '18 at 18:53
  • @gues271314 those examples are all not infinite though – timgfx Jan 21 '18 at 18:54
  • @timgfx How is the code at https://stackoverflow.com/questions/48370317/is-there-a-clean-way-to-infinitely-use-async-functions/48370382#comment83727606_48370382 related to the question? – guest271314 Jan 21 '18 at 18:54
  • _"those examples are all not infinite though"_ ? Yes they are, including the code at the second comment – guest271314 Jan 21 '18 at 18:55
  • @guest271314 ah I assumed that they werent infinite because they work. they work for some time because of callbacks. the code I posted is an iterating function without callbacks, which will instantly exceed the call stack limim and crash – timgfx Jan 21 '18 at 18:57
  • @timgfx No, it will not when coded properly. That is, as suggested by Chris Riebschlager using `while(true)` – guest271314 Jan 21 '18 at 18:58
  • @guest271314 while(true) isnt an iterating function. the reason i want to use a loop instead of an iterating function is that iterating functions crash after some time – timgfx Jan 21 '18 at 18:59
  • @timgfx It is not clear what the issue is. If you are iterating using a `for` or `for..of` loop how is "infinitely" related to the inquiry? – guest271314 Jan 21 '18 at 19:02