Skip to content

Instantly share code, notes, and snippets.

@roychri
Created August 21, 2020 14:20
Show Gist options
  • Save roychri/ae684e50d523cc4ea95d52fdb5bc1803 to your computer and use it in GitHub Desktop.
Save roychri/ae684e50d523cc4ea95d52fdb5bc1803 to your computer and use it in GitHub Desktop.
How async function react to Throw
function otherFunctionResolve() {
return Promise.resolve();
}
function otherFunctionReject() {
return Promise.reject();
}
function otherFunctionThrow() {
throw new Error("oops");
}
function doSomethingResolve() {
return otherFunctionResolve();
}
function doSomethingReject() {
return otherFunctionReject();
}
function doSomethingThrow() {
return otherFunctionThrow();
}
async function doSomethingThrowAsync() {
return otherFunctionThrow();
}
doSomethingResolve().then( () => console.log("PASSED") ).catch( () => console.log("FAILED") );
doSomethingReject().then( () => console.log("FAILED") ).catch( () => console.log("PASSED") );
try {
doSomethingThrow().then( () => console.log("FAILED") ).catch( () => console.log("FAILED") );
} catch ( err ) {
console.log("PASSED");
}
doSomethingThrowAsync().then( () => console.log("FAILED") ).catch( () => console.log("PASSED") );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment