Skip to content

Instantly share code, notes, and snippets.

@aaronblondeau
Created August 20, 2016 15:34
Show Gist options
  • Save aaronblondeau/38355067f85ef6fcc11a7d51994765e1 to your computer and use it in GitHub Desktop.
Save aaronblondeau/38355067f85ef6fcc11a7d51994765e1 to your computer and use it in GitHub Desktop.
Example of Bluebird's Promise.coroutine
var Promise = require('bluebird');
var request = require('request');
var fs = require('fs');
request_promise = Promise.promisify(request);
writeFile_promise = Promise.promisify(fs.writeFile);
Promise.coroutine(function* () {
try {
//I think this is way more readable:
var response = yield request_promise('https://s3.amazonaws.com/aaronblondeau/widgets/widgets.json');
var data = JSON.parse(response.body);
yield writeFile_promise('./widgets.json', JSON.stringify(data.widgets));
console.log("Done.");
}
catch(err) {
// Hey - cool a real live try-catch to handle our errors!
console.error(err);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment