effection icon indicating copy to clipboard operation
effection copied to clipboard

✨ add interval() helper to consume a stream of intervals

Open cowboyd opened this issue 4 months ago • 3 comments

Motivation

setInterval() is a core JavaScript API that allows you to setup global state. As such, there really isn't any reason that you shouldn't consume it using structured concurrency.

Approach

This adds an interval() function that constructs a stream that emits an item every time that the interval is triggered.

let startTime = Date.now();

for (let _ of yield* each(interval(10))) {
  let elapsed = Date.now() - startTime;
  console.log(`elapsed time: ${elapsed} ms`);
  yield* each.next();
}

cowboyd avatar Jun 19 '25 11:06 cowboyd