0

There are some similar questions about bluebird, jQuery, and Q deferreds and promises but those libraries have some subtle differences to the new, standard, built-in promises that came to JavaScript in the ES6 standard.

I'd like a nice canonical answer for how best to idiomatically code the various kinds of conditional looping constructs serially. That is, when the next iteration is only asynchronously initiated once the previous one asynchronously completed, and when the entire sequence is terminated when some condition is met, as with a do, for, while loop, etc.

I find the parallel and non conditional equivalents are easy and intuitive whereas many of us have trouble when we need loops which are conditional, serial, and asynchronous at once.

I'm not sure whether serial loops where the result of one operation is the input to the next require different idioms to serial loops done for other reasons, such as conserving resources or imposed by web APIs.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
  • _"That is, when the next iteration is only asynchronously initiated once the previous one asynchronously completed, and when the entire sequence is terminated when some condition is met"_ You can substitute using `Array.prototype.shift()` with recursion for loops; or recursion alone. – guest271314 Jul 06 '16 at 05:21
  • See http://stackoverflow.com/questions/33718545/promise-try-until-it-succeds/ , http://stackoverflow.com/questions/38034574/multiple-sequential-fetch-promise/ , – guest271314 Jul 06 '16 at 05:28
  • @Bergi: Which answer there does "exact duplicate" pertain to? Yours? The highest voted? Or the ES6 specific one? I don't see how it's canonical as the OP is specifically asking about Bluebird and most of the answers seem to address OP's specific use-case and code, making it quite unclear which parts are the canonical idiom that subsequent readers should exact and generalize to their needs. I can only assume that you must be referring to [kamran's answer](http://stackoverflow.com/a/38003741/527702) – hippietrail Jul 06 '16 at 08:41
  • @guest271314: I don't follow. – hippietrail Jul 06 '16 at 08:41
  • @Bergi: Then again, though kamran's answer is neat, concise, ES6-specific, and not full of OP-specific details, it does not cover *conditional* loops. It's just a serial `forEach` or `map` and not a `do`, `for`, or `while`. – hippietrail Jul 06 '16 at 09:05
  • Both links use recursion instead of `for` or `while` loops to return same result – guest271314 Jul 07 '16 at 04:16
  • @guest271314: Thanks for the clarification. In many comments on similar questions I found people speaking against recursion. Another reason a canonical answer would help. Using `Array.reduce()` seems to be a great way to avoid recursion that I found over and over too, but it's not obvious whether and how `reduce` can stop on a conditional, plus of course it depends on processing an array whereas `while` and `for` allow many other possibilities. – hippietrail Jul 07 '16 at 10:26
  • What is expected result? What are you trying to achieve? – guest271314 Jul 08 '16 at 03:24

0 Answers0