iterable-run
v1.1.17
Published
Collects all yielded items as well as the return result from an iterable or an async iterable.
Downloads
16
Maintainers
Readme
iterable-run
Features
- Supports both iterables and async iterables
- Preserves the final return value (
return) - Fully typed
- Well tested
Usage
reading sync iterable
import { iterableRun } from "iterable-run"
function* deepThought() {
yield "ultimate"
yield "answer"
return 42
}
const { items, result } = iterableRun(deepThought())
console.log(items) // ["ultimate", "answer"]
console.log(result) // 42reading async iterable
import { asyncIterableRun } from "iterable-run"
async function* range(n: number) {
for (let i = 0; i < n; i++) {
yield await Promise.resolve(i)
}
return await Promise.resolve("finished")
}
const { items, result } = await asyncIterableRun(range(5))
console.log(items) // [0, 1, 2, 3, 4]
console.log(result) // "finished"Related
If you don't need the return value from the iterable, consider using the standard:
Array.from(iterable)for synchronous iterablesArray.fromAsync(asyncIterable)for asynchronous iterables
These native methods collect yielded values into arrays but discard the final return result. That makes them simpler and potentially more appropriate if you only care about the items.
