-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
40 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Like `try {} finally {}` except: | ||
// - if both blocks throw, the exception from the `try` block has priority. | ||
// - the finally block cannot return a value. | ||
export const safeFinally = async function (tryFunc, finallyFunc) { | ||
const returnValue = await callTry(tryFunc, finallyFunc) | ||
await finallyFunc() | ||
return returnValue | ||
} | ||
|
||
const callTry = async function (tryFunc, finallyFunc) { | ||
try { | ||
return await tryFunc() | ||
} catch (error) { | ||
await callSilentFinally(finallyFunc) | ||
throw error | ||
} | ||
} | ||
|
||
const callSilentFinally = async function (finallyFunc) { | ||
try { | ||
await finallyFunc() | ||
} catch {} | ||
} |