1 # disallow control flow statements in `finally` blocks (no-unsafe-finally)
3 JavaScript suspends the control flow statements of `try` and `catch` blocks until the execution of `finally` block finishes. So, when `return`, `throw`, `break`, or `continue` is used in `finally`, control flow statements inside `try` and `catch` are overwritten, which is considered as unexpected behavior. Such as:
6 // We expect this function to return 1;
9 return 1; // 1 is returned but suspended until finally block ends
13 return 3; // 3 is returned before 1, which we did not expect
21 // We expect this function to throw an error, then return
24 throw new Error("Try"); // error is thrown but suspended until finally block ends
26 return 3; // 3 is returned before the error is thrown, which we did not expect
34 // We expect this function to throw Try(...) error from the catch block
37 throw new Error("Try")
39 throw err; // The error thrown from try block is caught and rethrown
41 throw new Error("Finally"); // Finally(...) is thrown, which we did not expect
45 // > Uncaught Error: Finally(...)
49 // We expect this function to return 0 from try block.
52 return 0; // 0 is returned but suspended until finally block ends
54 break label; // It breaks out the try-finally block, before 0 is returned.
64 This rule disallows `return`, `throw`, `break`, and `continue` statements inside `finally` blocks. It allows indirect usages, such as in `function` or `class` definitions.
66 Examples of **incorrect** code for this rule:
69 /*eslint no-unsafe-finally: "error"*/
70 let foo = function() {
82 /*eslint no-unsafe-finally: "error"*/
83 let foo = function() {
94 Examples of **correct** code for this rule:
97 /*eslint no-unsafe-finally: "error"*/
98 let foo = function() {
104 console.log("hola!");
110 /*eslint no-unsafe-finally: "error"*/
111 let foo = function() {
125 /*eslint no-unsafe-finally: "error"*/
126 let foo = function(a) {
142 ## When Not To Use It
144 If you want to allow control flow operations in `finally` blocks, you can turn this rule off.