-
-
Save joepie91/11e36819dcca49f54348 to your computer and use it in GitHub Desktop.
Error-tolerant promise map (Bluebird)
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 bhttp = require("bhttp"); | |
var urls = [ | |
"http://google.com/", | |
"http://yahoo.com/", | |
"http://bing.com/" // Ha ha, just kidding who uses Bing anyway :) | |
]; | |
Promise.try(function(){ | |
/* There's a faster shorthand for this, but for illustrative purposes, we'll just return the array here and pretend that it was generated by something else, somehow. */ | |
return urls; | |
}).map(function(url){ | |
return Promise.try(function(){ | |
return bhttp.get(url); | |
}).catch(function(err){ | |
return null; | |
}) | |
}).filter(function(response){ | |
return (response != null); | |
}).map(function(response){ | |
return response.body; | |
}).then(function(responseBodies){ | |
/* Now `responseBodies` is an array of response bodies, one for each URL, in the original URL order. */ | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment