https://github.com/borderless/defer
Tiny, type-safe, JavaScript-native `defer` implementation
https://github.com/borderless/defer
Last synced: 10 months ago
JSON representation
Tiny, type-safe, JavaScript-native `defer` implementation
- Host: GitHub
- URL: https://github.com/borderless/defer
- Owner: borderless
- License: mit
- Created: 2020-10-19T05:17:18.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-12-12T13:08:14.000Z (about 2 years ago)
- Last Synced: 2024-09-17T05:08:40.716Z (over 1 year ago)
- Language: TypeScript
- Size: 1.58 MB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Defer
> Tiny, type-safe, JavaScript-native `defer` implementation.
**Why?** It's helpful to have statements that can automatically execute when a function has completed. For example, to `close` a file descriptor or clear state.
## Installation
```sh
npm install @borderless/defer --save
```
## Usage
```ts
import { defer, deferSync } from "@borderless/defer";
let i = 0;
const fn = defer(async function* () {
const result = await doSomethingAsync();
yield () => {
i++;
expect(i).toEqual(1);
};
expect(i).toEqual(0);
return result;
});
await fn(); // `i == 1`.
```
If you're working with synchronous functions, you can use `deferSync` instead.
## License
MIT