count(): AsyncIterableIterator<number>
Makes an infinite async iterable of evenly spaced values starting with
the start number.
import { count } from "./infinite.ts"; const iterable = count(5); for await (const value of iterable) { console.log(value); }
The above example will print the following and keep going forever:
5 6 7 8 9 (...)
You could adjust the interval by passing a second argument to count():
import { count } from "./infinite.ts"; const iterable = count(0, 3); for await (const value of iterable) { console.log(value); }
The above example will print the following and keep going forever:
0 3 6 9 12 (...)
As it's infinite, it's usually used with break to stop the iteration:
import { count } from "./infinite.ts"; for await (const value of count(0)) { if (value > 4) break; console.log(value); }
Or with other async generators like takeWhile():
import { count } from "./infinite.ts"; import { takeWhile } from "./take.ts"; for await (const value of takeWhile(count(0), v => v <= 4)) { console.log(value); }
The both examples above will print the following 4 lines:
0 1 2 3
AsyncIterableIterator<number>
An async iterable of evenly spaced values.