itur

0.0.3 • Public • Published

Build Status codecov

Itur

Async made easy with generator control flow

$ npm i --save itur

Basic example

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)

API

forEach

Itur.forEach([1,2,3], function*(key, value) {
    yield Promise.resolve(key)
})

map

const result = yield Itur.map([1,2,3], function*(key, value) {
    return yield Promise.resolve(key)
})

console.log(result) // [0,1,2]

all

const result = yield Itur.all([1,2,3], function*(key, value) {
    return yield Promise.resolve(key)
})

console.log(result) // [0,1,2]

reduce

const result = yield Itur.reduce([1,2,3], function*(agg, key, value) {
     agg += yield Promise.resolve(value)
     return agg
}, 0)

console.log(result) // 6

find

const result = yield Itur.find([1,2,3], function*(agg, key, value) {
     return key === yield Promise.resolve(2)
}, 0)

console.log(result) // 3

while

let c = 0
Itur.while(
    () => c < 10, 
    function*() {
        yield Promise.resolve('do any async')
        c++
    }
)

range

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
}

Chaining

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

Constructor syntax

Readme

Keywords

none

Package Sidebar

Install

npm i itur

Weekly Downloads

1

Version

0.0.3

License

MIT

Last publish

Collaborators

  • francoispqt