Skip to content

Instantly share code, notes, and snippets.

@jasonmcaffee
Created February 9, 2016 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonmcaffee/127236774f9294453832 to your computer and use it in GitHub Desktop.
Save jasonmcaffee/127236774f9294453832 to your computer and use it in GitHub Desktop.
'use strict';
//error is caught by the promise
function someSyncFunctionThrow(){
let promise = new Promise((resolve, reject)=>{
throw 'someSyncFunctionThrow error occurred!!!!';
resolve();
});
return promise;
}
someSyncFunctionThrow().then(()=>{}).catch(err=>{ console.error(`Promise.catch -- error someSyncFunctionThrow: ${err}`)});
//error is caught by the promise
function someSyncFunctionReferrenceError(){
let promise = new Promise((resolve, reject)=>{
asdf.x();
resolve();
});
return promise;
}
someSyncFunctionReferrenceError().then(()=>{}).catch(err=>{console.error(`Promise.catch -- error someSyncFunctionReferrenceError ${err}`)});
//error is NOT caught by the promise.
function someAsyncFunctionThrow(){
let promise = new Promise((resolve, reject)=>{
setTimeout(()=>{
throw 'someAsyncFunctionThrow error occurred!!!!';
resolve();
},0);
});
return promise;
}
someAsyncFunctionThrow().then(()=>{}).catch(err=>{ console.error(`Promise.catch -- error someAsyncFunctionThrow: ${err}`)});
//error is NOT caught by the promise
function someAsyncFunctionReferenceError(){
let promise = new Promise((resolve, reject)=>{
setTimeout(()=>{
asdf.asdf();
resolve();
},0);
});
return promise;
}
someAsyncFunctionReferenceError().then(()=>{}).catch(err=>{console.error(`Promise.catch -- someAsyncFunctionReferenceError reference error ${err}`)});
//error is NOT caught by try catch.
function nonPromiseAsyncThrowWithCatchOutside(){
try{
setTimeout(()=>{
throw 'nonPromiseAsyncThrow error occurred!!!!';
},0)
}catch(e){
console.error(`catch caught exception ${e}`);
}
}
nonPromiseAsyncThrowWithCatchOutside();
//error is caught
function nonPromiseAsyncThrowWithCatchInside(){
setTimeout(()=>{
try{
throw 'nonPromiseAsyncThrow error occurred!!!!';
}catch(e){
console.error(`catch caught exception ${e}`);
}
},0);
}
nonPromiseAsyncThrowWithCatchInside();
//error is caught by promise
function someAsyncFunctionReferenceErrorWithTryCatch(){
let promise = new Promise((resolve, reject)=>{
setTimeout(()=>{
try{
asdf.asdf();
resolve();
}catch(e){
reject(e);
}
},0);
});
return promise;
}
someAsyncFunctionReferenceErrorWithTryCatch().then(()=>{}).catch(err=>{console.error(`Promise.catch -- someAsyncFunctionReferenceErrorWithTryCatch reference error ${err}`)});
process.on('uncaughtException', (err) => {
console.log(`Process Caught exception: ${err}`);
});
process.on('unhandledRejection', (reason, p) => {
console.log(`Process unhandledRejection ${reason}`);
});
process.on('rejectionHandled', (p) => {
console.log(`Process rejection handled`);
});
@jasonmcaffee
Copy link
Author

Result:

Promise.catch -- error someSyncFunctionThrow: someSyncFunctionThrow error occurred!!!!
Promise.catch -- error someSyncFunctionReferrenceError ReferenceError: asdf is not defined
Process Caught exception: someAsyncFunctionThrow error occurred!!!!
Process Caught exception: ReferenceError: asdf is not defined
Process Caught exception: nonPromiseAsyncThrow error occurred!!!!
catch caught exception nonPromiseAsyncThrow error occurred!!!!
Promise.catch -- someAsyncFunctionReferenceErrorWithTryCatch reference error ReferenceError: asdf is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment