delayeach

0.2.0 • Public • Published

delay each

delay each delayEach(arr, iterateFn(item, index, next, finish), finishFn)

install

npm install delayeach
var delayEach = require('delayeach')
var fs = require('fs')
var files = [
    './example/a.txt',
    './example/b.txt',
    './example/c.txt'
]
var contents = []
delayEach(
    files,
    function handle(file, index, next, finish) {
        fs.readFile(file, function (err, data) {
            var text
            if (err) {
                text = err.message
            }
            else {
                text = data.toString()
            }
            contents[index] = text
            if (err) {
                finish()
            }
            else {
                next()
            }
 
        })
    },
    function finish () {
        console.log('finish')
        console.log(JSON.stringify(contents))
    }
)
/**
 *   file: a.txt b.txt c.txt
 *   ["aaa\n","bbb\n","ccc\n"]
 */
 /**
  *  file: a.txt c.txt
  *  ["aaa\n","ENOENT: no such file or directory, open './example/b.txt'"]
  */

finish(message)

var delayEach = require('delayeach')
var arr = [1,2,3]
delayEach(arr, function (item, index, next, finish) {
    console.log(item)
    if (index === 1) {
        finish('index: 1')
    }
    next()
}, function finish(log) {
    switch(log) {
        case 'index: 1':
            console.log('do something...')
        break
        default:
            console.log('each all the data')
    }
})
/**
 * function delayEach - 延迟遍历
 * @param {array} task 
 * @param {taskHandle} 
 * @param {finishCallback} 
 */
/**
  * @callback - handle
  * @param {any} - value
  * @param {number} - index
  * @param {function} - next
  * @param {function} - finish
  */
 /**
  * @callback - finish
  */

Readme

Keywords

Package Sidebar

Install

npm i delayeach

Weekly Downloads

1

Version

0.2.0

License

none

Last publish

Collaborators

  • grifree
  • nimojs