@borderless/defer

Tiny, type-safe, JavaScript-native `defer` implementation.

Usage no npm install needed!

<script type="module">
  import borderlessDefer from 'https://cdn.skypack.dev/@borderless/defer';
</script>

README

Defer

NPM version NPM downloads Build status Test coverage Bundle size

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

npm install @borderless/defer --save

Usage

import { defer, deferSync } from "@borderless/defer";

let i = 0;

const fn = defer((defer) => {
  defer(() => {
    i++;
    expect(i).toEqual(1);
  });

  expect(i).toEqual(0);
});

await fn(); // `i == 1`.

If you're working with synchronous functions, you can use deferSync instead.

License

MIT