Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-abort-controller

Package Overview
Dependencies
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-abort-controller

AbortController for Node based on EventEmitter

  • 3.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12M
decreased by-1.55%
Maintainers
2
Weekly downloads
 
Created

What is node-abort-controller?

The node-abort-controller package is an implementation of the AbortController interface, which provides a way to abort one or more Web requests as and when desired. It is commonly used to cancel fetch requests or other asynchronous tasks that can be aborted.

What are node-abort-controller's main functionalities?

Creating an AbortController instance

This feature allows you to create a new instance of AbortController. You can then use the 'signal' property to pass the abort signal to functions that accept it.

const AbortController = require('node-abort-controller');
const controller = new AbortController();
const { signal } = controller;

Aborting a fetch request

This code sample demonstrates how to use the AbortController to abort an ongoing fetch request. The fetch request is initiated with the abort signal, and if the request is aborted, it will throw an 'AbortError'.

const fetch = require('node-fetch');
const controller = new AbortController();
const { signal } = controller;

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();

Using AbortController with async/await

This example shows how to use AbortController with async/await syntax. A fetch request is made, and if it is not completed within 5 seconds, the AbortController is used to abort the request.

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

async function fetchData(url) {
  const controller = new AbortController();
  const { signal } = controller;
  setTimeout(() => controller.abort(), 5000); // Abort after 5 seconds

  try {
    const response = await fetch(url, { signal });
    return await response.json();
  } catch (err) {
    if (err.name === 'AbortError') {
      console.log('Fetch aborted');
    } else {
      throw err;
    }
  }
}

fetchData('https://example.com').catch(console.error);

Other packages similar to node-abort-controller

Keywords

FAQs

Package last updated on 26 Jan 2023

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc