New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

abortium

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abortium

Halt a process of asynchronous steps using an AbortSignal

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

Abortium

Halt a process of asynchronous steps using an AbortSignal.

Why

JavaScript is single-threaded, which means you can't really halt a running process. However, it provides us an API called AbortController that is basically usable only in specific scenarios and APIs, such as fetch and addEventListener.

If you want to use the AbortController API in your code, you need to wait for each function to finish, and check between the function runs whether the AbortSignal has been aborted. This is a tedious task, and it can be easily forgotten:

const controller = new AbortController();

async function processData() {
  const result1 = await heavyDataProcessing(data, controller.signal);

  if (controller.signal.aborted) {
    return;
  }

  const result2 = await heavyDataProcessing(result1, controller.signal);

  if (controller.signal.aborted) {
    return;
  }

  const result3 = await heavyDataProcessing(result2, controller.signal);

  if (controller.signal.aborted) {
    return;
  }

  const result4 = await heavyDataProcessing(result3, controller.signal);

  if (controller.signal.aborted) {
    return;
  }

  return result4;
}

button.addEventListener('click', () => {
  controller.abort();
});

What

Abortium solves this problem by letting you create a process of asynchronous steps that will be halted when the AbortSignal is aborted. It uses the AbortController API under the hood, and provides a more convenient API to use:

import { abortableProcess } from 'abortium';

const controller = new AbortController();

async function processData() {
  const process = abortableProcess(controller.signal)
    .then((_, signal) => heavyDataProcessing(data, signal))
    .then((result1, signal) => heavyDataProcessing(result1, signal))
    .then((result2, signal) => heavyDataProcessing(result2, signal))
    .then((result3, signal) => heavyDataProcessing(result3, signal));

  const finalResult = await process.execute();

  return finalResult;
}

It's somewhat similar to the Promise API. You chain the steps using .then(), and the library gives you the previous step's result and the AbortSignal as arguments.

Each step in the chain is typed, so the next step will have the correct type for the previous step's result.

When the AbortSignal is aborted, the process will be halted, and all queued steps will be skipped.

FAQs

Package last updated on 04 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts