Async made easy with generator control flow
$ npm i --save itur
const Itur = require('itur')
const main = function*() {
const result = yield Itur.reduce([1,2,3], function*(agg, key, value){
agg += yield Promise.resolve(value)
return agg
}, 0)
console.log(result) // 6
}
Itur.run(main)()
.catch(console.error)
Itur.forEach([1,2,3], function*(key, value) {
yield Promise.resolve(key)
})
const result = yield Itur.map([1,2,3], function*(key, value) {
return yield Promise.resolve(key)
})
console.log(result) // [0,1,2]
const result = yield Itur.all([1,2,3], function*(key, value) {
return yield Promise.resolve(key)
})
console.log(result) // [0,1,2]
const result = yield Itur.reduce([1,2,3], function*(agg, key, value) {
agg += yield Promise.resolve(value)
return agg
}, 0)
console.log(result) // 6
const result = yield Itur.find([1,2,3], function*(agg, key, value) {
return key === yield Promise.resolve(2)
}, 0)
console.log(result) // 3
let c = 0
Itur.while(
() => c < 10,
function*() {
yield Promise.resolve('do any async')
c++
}
)
const a = [1,2,3]
const fn = function*(key, value) {
return yield Promise.resolve('foobar')
}
for (const i of Itur.range(a, fn)) {
console.log(i) // foobar
}
You can chain certain operations. Example:
const result = yield Itur.map([1,2,3], function*(key, value) {
return key + yield Promise.resolve(value)
})
.reduce(function*(agg, key, value) {
agg.push(yield Promise.resolve(value))
return agg
}, [])
.find(function*(key, value) {
return key === 1
})
console.log(result) // 3