Path: blob/master/node_modules/asynckit/lib/iterate.js
1126 views
var async = require('./async.js')1, abort = require('./abort.js')2;34// API5module.exports = iterate;67/**8* Iterates over each job object9*10* @param {array|object} list - array or object (named list) to iterate over11* @param {function} iterator - iterator to run12* @param {object} state - current job status13* @param {function} callback - invoked when all elements processed14*/15function iterate(list, iterator, state, callback)16{17// store current index18var key = state['keyedList'] ? state['keyedList'][state.index] : state.index;1920state.jobs[key] = runJob(iterator, key, list[key], function(error, output)21{22// don't repeat yourself23// skip secondary callbacks24if (!(key in state.jobs))25{26return;27}2829// clean up jobs30delete state.jobs[key];3132if (error)33{34// don't process rest of the results35// stop still active jobs36// and reset the list37abort(state);38}39else40{41state.results[key] = output;42}4344// return salvaged results45callback(error, state.results);46});47}4849/**50* Runs iterator over provided job element51*52* @param {function} iterator - iterator to invoke53* @param {string|number} key - key/index of the element in the list of jobs54* @param {mixed} item - job description55* @param {function} callback - invoked after iterator is done with the job56* @returns {function|mixed} - job abort function or something else57*/58function runJob(iterator, key, item, callback)59{60var aborter;6162// allow shortcut if iterator expects only two arguments63if (iterator.length == 2)64{65aborter = iterator(item, async(callback));66}67// otherwise go with full three arguments68else69{70aborter = iterator(item, key, async(callback));71}7273return aborter;74}757677