Skip to content

Instantly share code, notes, and snippets.

@cdolek
Created June 30, 2018 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdolek/3b092e1559409a87e6e84bb50ad11bf1 to your computer and use it in GitHub Desktop.
Save cdolek/3b092e1559409a87e6e84bb50ad11bf1 to your computer and use it in GitHub Desktop.
Meteor multiple functions & future(s) result
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