Skip to content

Instantly share code, notes, and snippets.

@amitu
Last active August 29, 2015 14:23
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 amitu/6de606aa5671add11978 to your computer and use it in GitHub Desktop.
Save amitu/6de606aa5671add11978 to your computer and use it in GitHub Desktop.
node, Q and generators
// $ npm install q
// $ node --harmony tyield.js
var Q = require("q");
function timeoutPromose(delay) {
var d = Q.defer();
setTimeout(function(){
d.resolve("done");
}, delay);
return d.promise;
}
function *timed() {
console.log("going to sleep for 1");
yield timeoutPromose(1000);
console.log("now we are talking")
yield timeoutPromose(2000);
console.log("done");
}
Q.async(timed)();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment