-
Notifications
You must be signed in to change notification settings - Fork 0
/
allInParallel.js
42 lines (34 loc) · 883 Bytes
/
allInParallel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
var bluebird = require('bluebird');
var util = require('util');
var assert = require('assert');
/**
* typical callback
*/
function calculatorCB(input, outputCallback) {
util.log('calP', input);
setTimeout(function () {
util.log('calP done', input);
outputCallback(undefined, input * 10);
}, 1000)
}
/**
* promisify typical callback function.
*/
var calP = bluebird.promisify(calculatorCB);
function* calc(input) {
return (yield calP(input))
}
var test = bluebird.coroutine(function* () {
var result1 = yield [calP(1), calP(2)];
//var result1 = yield* runParallel(calc.bind(undefined,1), calc.bind(undefined,2));
util.log(result1);
});
function* runParallel(){
var promises = [];
for(var i = 0; i < arguments.length; i++){
promises.push(bluebird.coroutine(arguments[i])())
}
return yield bluebird.all(promises);
}
test();