effection
effection copied to clipboard
✨ add interval() helper to consume a stream of intervals
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();
}