Run Futures in parallel
const Task = require('data.task');
const parallel = require('parallel-future')(Task);
const parallelRequests = parallel([ getUsers, getPosts ]);
parallelRequests.fork(onRejected, (results) => {
console.log('Users', results[0]);
console.log('Posts', results[1]);
});
R.sequence(Task.of)
?Because it only appears to run the Futures
in parallel.
Run the tests and see for your self.
describe('runs parallel', () => {
it('should run Futures in parallel', done => {
console.time('parallel-future');
parallel([ time(1)
, time(2)
, time(3)
]).fork(_, eventuallyEqual([1, 2, 3], () => { console.timeEnd('parallel-future'); done() }));
});
it('compared to R.sequence (only appears to be parallel)', done => {
console.time('sequence');
R.sequence(Task.of)
([ time(1)
, time(2)
, time(3)
]).fork(_, eventuallyEqual([1, 2, 3], () => { console.timeEnd('sequence'); done() }));
});
});
Output:
runs parallel
parallel-future: 106ms
✓ should run Futures in parallel (106ms)
sequence: 315ms
✓ compared to R.sequence (only appears to be parallel) (316ms)
parallelFuture :: Future -> [Future a] -> Future [a]
MIT © stoeffel