Ever wanted to use async functions in Array.map? This library may help you to do that.
import { transform } from "lazy-transform"
const iterable = transform([1, 2, 3])
.mapAwait(async value => {
// Some API calls here
// ...
return valueFromApi
})
for await (const value of iterable) {
console.log(value)
}
$ yarn add lazy-transform
Wrap an Iterable
or AsyncIterable
so library methods can be called to them. The wrapper itself is also an iterable.
const iterable = transform([1, 2, 3])
function* generator() {
yield* [1, 2, 3]
}
const iterable = transform(generator())
for (const value of iterable) {
// ...
}
async function* asyncGenerator() {
yield* [1, 2, 3]
}
const asyncIterable = transform(asyncGenerator())
for await (const value of asyncIterable) {
// ...
}
Create a new iterable with the results of calling func
or asyncFunc
on every item in the previous iterable.
const iterable = transform([1, 2, 3]).map(value => value + 5)
const asyncIterable = transform([1, 2, 3]).mapAwait(async value => value + 5)
- Add
.collect
method which returns an array or something. - Add more methods, like
.reduce
,.peek
, etc. - Add
Scheduler
concept for every awaiters so passed async functions can be called not just in serial.