Last change
on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1017 bytes
|
Line | |
---|
1 | var iterate = require('./lib/iterate.js')
|
---|
2 | , initState = require('./lib/state.js')
|
---|
3 | , terminator = require('./lib/terminator.js')
|
---|
4 | ;
|
---|
5 |
|
---|
6 | // Public API
|
---|
7 | module.exports = parallel;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Runs iterator over provided array elements in parallel
|
---|
11 | *
|
---|
12 | * @param {array|object} list - array or object (named list) to iterate over
|
---|
13 | * @param {function} iterator - iterator to run
|
---|
14 | * @param {function} callback - invoked when all elements processed
|
---|
15 | * @returns {function} - jobs terminator
|
---|
16 | */
|
---|
17 | function parallel(list, iterator, callback)
|
---|
18 | {
|
---|
19 | var state = initState(list);
|
---|
20 |
|
---|
21 | while (state.index < (state['keyedList'] || list).length)
|
---|
22 | {
|
---|
23 | iterate(list, iterator, state, function(error, result)
|
---|
24 | {
|
---|
25 | if (error)
|
---|
26 | {
|
---|
27 | callback(error, result);
|
---|
28 | return;
|
---|
29 | }
|
---|
30 |
|
---|
31 | // looks like it's the last one
|
---|
32 | if (Object.keys(state.jobs).length === 0)
|
---|
33 | {
|
---|
34 | callback(null, state.results);
|
---|
35 | return;
|
---|
36 | }
|
---|
37 | });
|
---|
38 |
|
---|
39 | state.index++;
|
---|
40 | }
|
---|
41 |
|
---|
42 | return terminator.bind(state, callback);
|
---|
43 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.