{"library":"node-abort-controller","title":"Node AbortController Polyfill","description":"node-abort-controller provides a minimal polyfill for the AbortController and AbortSignal Web APIs, specifically designed for Node.js environments running versions 14.6.x and below. It leverages Node.js's EventEmitter for its implementation. This package is explicitly *not* needed for Node.js versions 14.7.0 and above, as `AbortController` and `AbortSignal` are built-in globals in modern Node.js environments, becoming stable in v15.4.0. The library's current stable version is 3.1.1, and its release cadence is tied to the evolving Node.js core, with updates typically occurring when there are breaking changes or new Node.js versions make the polyfill redundant. Its key differentiator is its lightweight, Node.js-specific approach, aiming to avoid shipping unnecessary polyfills to environments that don't require them, making it ideal for library authors targeting mixed Node.js and modern browser environments where `node-fetch` is also used.","language":"javascript","status":"maintenance","last_verified":"Tue Apr 21","install":{"commands":["npm install node-abort-controller"],"cli":null},"imports":["import { AbortController } from 'node-abort-controller';","import { AbortController, AbortSignal } from 'node-abort-controller';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import fetch from 'node-fetch';\nimport { AbortController } from 'node-abort-controller';\n\nconst main = async () => {\n  const controller = new AbortController();\n  const signal = controller.signal;\n\n  // Abort fetch after 500ms. Effectively a timeout\n  const timeoutId = setTimeout(() => {\n    console.log('Request timed out, aborting...');\n    controller.abort();\n  }, 500);\n\n  try {\n    console.log('Fetching from Google...');\n    const response = await fetch('https://www.google.com', { signal });\n    clearTimeout(timeoutId);\n    if (response.ok) {\n      console.log(`Successfully fetched Google (Status: ${response.status})`);\n      // const text = await response.text();\n      // console.log(text.substring(0, 100) + '...');\n    } else {\n      console.error(`Failed to fetch Google (Status: ${response.status})`);\n    }\n  } catch (error) {\n    if (error.name === 'AbortError') {\n      console.warn('Fetch request was aborted.');\n    } else {\n      console.error('Fetch error:', error.message);\n    }\n  }\n};\n\nmain();","lang":"javascript","description":"This example demonstrates how to use AbortController with `node-fetch` to implement a timeout for an HTTP request, showing how to create a controller, pass its signal, and handle the abort event.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}