0

As the title suggests, I want to delay each value in an iterable by some amount of time while keeping the iterable lazily evaluated. Here's the closest I've got so far which works fine for finite iterables or those which don't throw an error

function* iter () {
  let i = 0
  while (true) yield i++
}

rxjs.zip(
  rxjs.from(iter()),
  rxjs.timer(500, 500),
  x => x
).subscribe(console.log)
  • 1
    The answer here provides an example of this: https://stackoverflow.com/questions/42120680/how-does-rxjs-mergemap-work The example is titled `implement .delay with .mergeMap` – DeborahK Nov 13 '19 at 17:06

1 Answers1

0

Another way might be:

const source$ = interval(0);
source$.pipe(
    concatMap(x => 
        of(x).pipe(delay(500))
    )
).subscribe(console.log);
Mauro Aguilar
  • 1,093
  • 13
  • 22
  • This doesn't seem to work for when `source$` is set to an infinite generator `$source = from((function* () { let i = 0; while (true) yield i++ })())` – Tom Sherman Nov 14 '19 at 10:10
  • well, using an infinite loop for that doesn't seem to be a smart idea, wouldn't that cause a maximum call stack error? I'm not sure if the generators are able to prevent that from happening, I'm trying to test that behavior in stackblitz but it kills the browser in the output panel https://stackblitz.com/edit/rxjs-kzlifr?devtoolsheight=60 – Mauro Aguilar Nov 14 '19 at 19:16
  • The whole idea of a generator is that they are lazily evaluated so you don't get an infinite loop, and a call stack is not relevant here. Infinite generators have their uses, see: haskells ranges or this: https://github.com/tom-sherman/orangutan/blob/master/lib/range-factory.ts – Tom Sherman Nov 14 '19 at 20:32