-
-
Save joepie91/2b62b735020e51b260abacaa133f48f0 to your computer and use it in GitHub Desktop.
Errors 'bubbling up the chain' with Promises
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
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