Created
September 6, 2023 17:46
-
-
Save Dionid/e4b7fe583206672c4a64e36e086be860 to your computer and use it in GitHub Desktop.
Go defer in JS by try ... finally ...
This file contains hidden or 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
describe('finally', () => { | |
it('1', () => { | |
const ttt = () => { | |
try { | |
return true | |
} finally { | |
// eslint-disable-next-line no-unsafe-finally | |
return false | |
} | |
} | |
expect(ttt()).toBe(false) | |
}) | |
it('2', () => { | |
let res = false | |
const ttt = () => { | |
try { | |
return false | |
} finally { | |
res = true | |
} | |
} | |
expect(ttt()).toBe(false) | |
expect(res).toBe(true) | |
}) | |
it('3', () => { | |
const ttt = () => { | |
let res = false | |
try { | |
return res | |
} finally { | |
res = true | |
} | |
} | |
const res = ttt() | |
expect(res).toBe(false) | |
}) | |
it('4', async () => { | |
let res = false | |
const ttt = async () => { | |
try { | |
return res | |
} finally { | |
res = true | |
} | |
} | |
expect(await ttt()).toBe(false) | |
expect(res).toBe(true) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment