Socket
Socket
Sign inDemoInstall

abort-controller

Package Overview
Dependencies
1
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    abort-controller

An implementation of WHATWG AbortController interface.


Version published
Weekly downloads
16M
increased by2.29%
Maintainers
1
Install size
261 kB
Created
Weekly downloads
 

Package description

What is abort-controller?

The abort-controller npm package provides a way to abort one or more Web API tasks, such as fetch requests, by using an AbortController object. This package is a polyfill for the AbortController interface based on the WHATWG Fetch specification, which allows you to cancel tasks by calling the abort() method on the controller instance.

What are abort-controller's main functionalities?

Creating an AbortController instance

This feature allows you to create a new instance of AbortController, which can be used to generate a signal that can be passed to fetch or other APIs that support aborting.

const AbortController = require('abort-controller');
const controller = new AbortController();
const signal = controller.signal;

Aborting a fetch request

This code sample demonstrates how to use the AbortController to abort an ongoing fetch request. The signal is passed to the fetch call, and the request can be aborted by calling the abort() method on the controller.

const fetch = require('node-fetch');
const AbortController = require('abort-controller');

const controller = new AbortController();
const signal = controller.signal;

fetch('https://example.com', { signal })
  .then(response => response.json())
  .catch(err => {
    if (err.name === 'AbortError') {
      console.log('Fetch aborted');
    } else {
      console.error('Fetch error:', err);
    }
  });

// Abort the request
controller.abort();

Other packages similar to abort-controller

Readme

Source

abort-controller

npm version Downloads/month Build Status Coverage Status Dependency Status

An implementation of WHATWG AbortController interface.

import AbortController from "abort-controller"

const controller = new AbortController()
const signal = controller.signal

signal.addEventListener("abort", () => {
    console.log("aborted!")
})

controller.abort()

https://jsfiddle.net/1r2994qp/1/

💿 Installation

Use npm to install then use a bundler.

npm install abort-controller

Or download from dist directory.

📖 Usage

Basic

import AbortController from "abort-controller"
// or
const AbortController = require("abort-controller")

// or UMD version defines a global variable:
const AbortController = window.AbortControllerShim

If your bundler recognizes browser field of package.json, the imported AbortController is the native one and it doesn't contain shim (even if the native implementation was nothing). If you wanted to polyfill AbortController for IE, use abort-controller/polyfill.

Polyfilling

Importing abort-controller/polyfill assigns the AbortController shim to the AbortController global variable if the native implementation was nothing.

import "abort-controller/polyfill"
// or
require("abort-controller/polyfill")

API

AbortController

https://dom.spec.whatwg.org/#interface-abortcontroller

controller.signal

The AbortSignal object which is associated to this controller.

controller.abort()

Notify abort event to listeners that the signal has.

📰 Changelog

🍻 Contributing

Contributing is welcome ❤️

Please use GitHub issues/PRs.

Development tools

  • npm install installs dependencies for development.
  • npm test runs tests and measures code coverage.
  • npm run clean removes temporary files of tests.
  • npm run coverage opens code coverage of the previous test with your default browser.
  • npm run lint runs ESLint.
  • npm run build generates dist codes.
  • npm run watch runs tests on each file change.

Keywords

FAQs

Last updated on 30 Mar 2019

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc