Do you know of any way to skip the execution of a finally block?
function fn(){
try{
return 1;
}finally{
console.log(2);
}
}
fn();
// Logs 2
// Returns 1
A bit more bizarre example would be if you use return in finally:
function fn(){
try{
return 1;
}finally{
return 2;
}
}
fn(); // 2
It seems like finally cannot be stopped!
Top comments (2)
The point of
finallyis to run after atry...catchblock no matter what happens. From the MDNShort of killing the process, calling another function within try{..}, or doing a while(true) within try{...}, the finally block will execute, period.