Skip to content

Instantly share code, notes, and snippets.

@joepie91
Last active August 1, 2016 19:06
Show Gist options
  • Save joepie91/2b62b735020e51b260abacaa133f48f0 to your computer and use it in GitHub Desktop.
Save joepie91/2b62b735020e51b260abacaa133f48f0 to your computer and use it in GitHub Desktop.
Errors 'bubbling up the chain' with Promises
function makeRequest(url) {
return Promise.try(() => {
return bhttp.get(url); // Whether an error occurs here...
}).then((response) => {
return response.body; // ... or here...
});
}
function makeManyRequests(urls) {
return Promise.try(() => {
if (urls.length === 0) {
throw new Error("Must specify at least one URL"); // ... or here...
} else {
return urls;
}
}).map((url) => {
return makeRequest(url);
});
}
function doMyThing() {
return Promise.try(() => {
return makeManyRequests([
"http://google.com/",
"http://bing.com/",
"http://yahoo.com"
]);
}).catch((err) => {
console.log("Oh noes!", err); // ... it will always end up here.
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment