Created
August 20, 2016 15:34
-
-
Save aaronblondeau/38355067f85ef6fcc11a7d51994765e1 to your computer and use it in GitHub Desktop.
Example of Bluebird's Promise.coroutine
This file contains 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
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