@borderless/defer
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

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

npm install @borderless/defer --save

Usage

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

Package Sidebar

Install

npm i @borderless/defer

Weekly Downloads

1

Version

2.0.1

License

MIT

Unpacked Size

9.16 kB

Total Files

6

Last publish

Collaborators

  • blakeembrey