Created
June 30, 2018 22:53
-
-
Save cdolek/3b092e1559409a87e6e84bb50ad11bf1 to your computer and use it in GitHub Desktop.
Meteor multiple functions & future(s) result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const range = [ | |
future => { | |
Meteor.setTimeout(() => { | |
future.return(1000); | |
}, 1000); | |
}, | |
future => { | |
Meteor.setTimeout(() => { | |
future.return(2000); | |
}, 2000); | |
}, | |
future => { | |
Meteor.setTimeout(() => { | |
future.return(3000); | |
}, 3000); | |
}, | |
]; | |
// iterate sequentially over the range to launch tasks | |
const futures = _.map(range, (task, index) => { | |
const future = new Future(); | |
console.log('launching task', index); | |
task(future); | |
// accumulate asynchronously parallel tasks | |
return future; | |
}); | |
// iterate sequentially over the tasks to resolve them | |
const results = _.map(futures, (future, index) => { | |
// waiting until the future has return | |
const result = future.wait(); | |
console.log('result from task', index, 'is', result, 'prev', index > 0 ? futures[index - 1].value : 'none'); | |
// accumulate results | |
return result; | |
}); | |
// | |
console.log(results); | |
return results; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment