Skip to content

Instantly share code, notes, and snippets.

@joepie91
Last active November 11, 2015 04:34
Show Gist options
  • Save joepie91/4c125c45ee6c5ea0375f to your computer and use it in GitHub Desktop.
Save joepie91/4c125c45ee6c5ea0375f to your computer and use it in GitHub Desktop.
Bluebird map + bhttp
var Promise = require("bluebird");
var bhttp = require("bhttp");
Promise.try(function(){
return bhttp.get("http://somesite.com/all-the-urls.txt");
}).then(function(response){
return response.body.toString().split("\n");
}).map(function(url){
return bhttp.get(url);
}).map(function(response){
return response.body;
}).then(function(arrayOfResponses){
/* arrayOfResponses now contains all the HTTP responses for each URL in the all-the-urls.txt file! */
}).catch(function(err){
/* You would normally NOT have a catch-all error handler like this - this is purely for the sake
* of example. Normally, your error handlers would only catch those errors that it is specfically
* interested in; an example of this is at https://gist.github.com/joepie91/f6a56acdae303e90e44a */
console.log("An error occurred!", err);
})
@dunielpls
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment