co-iterator


License
MIT
Install
npm install co-iterator@1.0.0

Documentation

co-iterator

Feature

co-iterator accepts an array and a method , then return an new array that contains 'thunkify' functions, so that you can yield this array directly and executed in parallel.

How to use

var iterator = require('co-iterator');
var co = require('co');

co(function* () {
    //define an array
    var array = [1, 2, 3];
    //define a method that every item in array should be executed
    //this function should have callback function as the last argument
    //and must callback with at least one argument. However, the first one argument means error or null
    var method = function (item,callback) {
        var time = Math.ceil(Math.random()*10)*1000;
        console.log('I will run after ',time,'s');
        setTimeout(()=>{
            var result = Promise.resolve(item);
            callback(null,result);
            console.log(time,'finish')
        },time)
    };
    //co-iterator will return an array with thunk function ,so that you can yield it directly
    var res = yield iterator(array,method);

    console.log(res); // => [1, 2, 3]

}).catch((err)=>{console.error(err)});