Code Monkey home page Code Monkey logo

abortify's Introduction

abortify

Create abortable async functions with ease

Abortable

An abortable is an asynchronous function that can be aborted. An abortable is passed a resolve and reject callback, just like the Promise constructor, and returns a function for aborting the operation. Consider the following definition of an abortable version of setTimeout:

import type { Abortable } from "abortify";

export function setAbortableTimeout(ms: number): Abortable<void> {
  return (resolve, reject) => {
    const handle = setTimeout(resolve, ms);
    return () => clearTimeout(handle);
  };
}

abortify

The abortify function converts abortable functions into functions that return a Promise. It works just like util.promisify, with a few additions:

  • The function will accept an AbortSignal as an optional final argument.
  • The function will throw an AbortError when the signal is aborted.
  • The abortable will be aborted before the promise resolves or rejects.

Consider the following definition of an abortified version of the setAbortableTimeout above:

import { abortify } from "abortify";

export const sleep = abortify(setAbortableTimeout);

// The sleep function above has the following signature:
// (ms: number, signal?: AbortSignal) => Promise<void>

Using abortified functions

Once you have abortified a function you use it just like any other async function. Consider the following example:

import { sleep } from "abortify";

// this function will print a message every second
// this function will only exit when the signal is aborted
async function forever(signal?: AbortSignal) {
  for (;;) {
    await sleep(1000, signal);
    console.log("another second has passed");
  }
}

async function main() {
  const controller = new AbortController();

  // abort after a short amount of time
  setTimeout(() => controller.abort(), 5000);

  try {
    await forever(controller.signal);
  } catch (err) {
    if (err.aborted) {
      console.error(err); // AbortError: The operation was aborted
    }
  }
}

main();

abortify's People

Contributors

bakerface avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.