Skip to content

Instantly share code, notes, and snippets.

@Dionid
Created September 6, 2023 17:46
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 Dionid/e4b7fe583206672c4a64e36e086be860 to your computer and use it in GitHub Desktop.
Save Dionid/e4b7fe583206672c4a64e36e086be860 to your computer and use it in GitHub Desktop.
Go defer in JS by try ... finally ...
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