Skip to content

Instantly share code, notes, and snippets.

@philip-goh
Created April 25, 2018 08:29
Show Gist options
  • Save philip-goh/eae41836c05424136c67f5df819b7519 to your computer and use it in GitHub Desktop.
Save philip-goh/eae41836c05424136c67f5df819b7519 to your computer and use it in GitHub Desktop.
Javascript promises and exception handling
// Just a simple test to see how exception handling works in a promise.
function someFunc() {
return new Promise((fulfil, reject) => {
fulfil("This is from a promise");
});
}
someFunc()
.then(res => {
console.log(`Result: ${res}`);
})
.then(() => {
console.log("In another then...");
throw new Error("This is an error");
})
.then(() => {
console.log("Will never get here...");
})
.catch(err => {
console.log(`Caught error: ${err}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment