0.0.2 • Published 7 years ago
lazy-transform v0.0.2
lazy-transform-js
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)
}Installation
$ yarn add lazy-transformAPI
transform(iterable)
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) {
// ...
}Wrapper.map(func) or .mapAwait(asyncFunc)
Create a new iterable with the results of calling func or asyncFuncon 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)Improvements
- Add
.collectmethod which returns an array or something. - Add more methods, like
.reduce,.peek, etc. - Add
Schedulerconcept for every awaiters so passed async functions can be called not just in serial.