Last active
January 29, 2022 22:03
-
-
Save rostegg/8a468ff76d61453c067d6e04c5f1a17c to your computer and use it in GitHub Desktop.
Async disposer pattern example (simple way to controll freeing a resource)
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
const withResource = async (fn) => { | |
const instance = await getInstance() | |
try { | |
return await fn(instance) | |
} finally { | |
await instance.close() | |
} | |
} | |
const task = await witchResource(async (instance) => { | |
const data = await instance.getData() | |
return data | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment