Skip to content

Instantly share code, notes, and snippets.

@matfin
Created August 4, 2015 14:01
Show Gist options
  • Save matfin/bfce7115fbeb33848916 to your computer and use it in GitHub Desktop.
Save matfin/bfce7115fbeb33848916 to your computer and use it in GitHub Desktop.
/**
* A practical example of using Fibers in Meteor JS
*/
var MeteorFiber = {
takeLong: function() {
var future = new this.Future();
Meteor.setTimeout(function() {
/**
* Something that could take a while...
*/
future.return('done');
}, 750);
return future.wait();
},
loopLong: function() {
var future = new this.Future(),
things = [1,2,3,4,5,6];
console.log('Before we run!');
/**
* This loop will run in sequence
*/
things.forEach(function(thing) {
this.takeLong();
console.log('Going to: ', thing);
}.bind(this));
/**
* And this will not be called until the above loop is finished
*/
console.log('After we run!');
future.return('I am returning!');
return future.wait();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment