Created
August 4, 2015 14:01
-
-
Save matfin/bfce7115fbeb33848916 to your computer and use it in GitHub Desktop.
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
/** | |
* 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