Giter Club home page Giter Club logo

node-abort-controller's Introduction

node-abort-controller

AbortController Polyfill for Node.JS based on EventEmitter

Build Status

Usage

import fetch from "node-fetch";
import { AbortController } from "node-abort-controller";

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

await fetch("https:/www.google.com", { signal });

// Abort after 500ms. Effectively a timeout
setTimeout(() => controller.abort(), 500);

Why would I use this?

You might not need to! Generally speaking, there are three environments your JavaScript code can run in:

  • Node
  • Modern Browsers (Not Internet Explorer)
  • Legacy Browsers (Mostly Internet Explorer)

For modern JS APIs, each environment would ideally get a polyfill:

  • only if it needs one
  • specific to the platform.

In practice, this is hard. Tooling such as webpack and browserify are great at making sure stuff works out of the box in all environments. But it is quite easy to fail on both points above. In all likelyhood, you end up shipping less than ideal polyfills on platforms that don't even need them. So what is a developer to do? In the case of fetch and AbortController I've done the work for you. This is a guide to that work.

If you are building a ...

Application running in modern browsers:

Congrats! You don't need a library or polyfill at all! Close this tab. Uninstall this package.

Application running in modern browsers AND node (such as a server side rendered JS app):

Use this package and node-fetch. It is minimally what you need.

Application supporting legacy browsers AND NOT node:

Use abort-controller and whatwg-fetch. These are more complete polyfills that will work in all browser environments.

Application supporting legacy browsers AND node:

Use abort-controller and cross-fetch. Same as above, except cross-fetch will polyfill correctly in both the browser and node.js

Library being consumed by other applications and using fetch internally:

Use this package and node-fetch. It is the smallest and least opinionated combination for your end users. Application developers targeting Internet Exploer will need to polyfill AbortController and fetch on their own. But your library won't be forcing unecessary polyfills on developers who only target modern browsers.

Goals

With the above guide in mind, this library has a very specific set of goals:

  1. Provide a minimal polyfill in node.js
  2. Do not provide a polyfill in any browser environment

This is the ideal for library authors who use fetch and AbortController internally and target both browser and node developers.

Prior Art

Thank you @mysticatea for https://github.com/mysticatea/abort-controller. It is a fantastic AbortController polyfill and ideal for many use cases.

node-abort-controller's People

Contributors

arthurschreiber avatar atassis avatar bisubus avatar dependabot[bot] avatar sdrsdr avatar southpolesteve 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.