node-fetch: why is `signal` recommended over `timeout`?

Viewed 4826

The node-fetch documentation suggests that using signal is recommended over timeout, but doesn't offer any hints as to why:

{
    // These properties are part of the Fetch Standard
    ...
    signal: null,       // pass an instance of AbortSignal to optionally abort requests
 
    // The following properties are node-fetch extensions
    ...
    timeout: 0,         // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies). Signal is recommended instead.
    ...
}

(source: https://www.npmjs.com/package/node-fetch)

Why is that? In what situations would it be a problem to use timeout?

2 Answers

The timeout option is a node-fetch-proprietary extension that is not part of the WhatWG fetch standard.

When it was built, AbortController did not exist in the standard yet, so the library offered timeout as a workaround.

Adding a non-standard feature to a library that is supposed to implement the standard as exactly as possible has problems:

  • Extra features tend to 'creep out' of the library when people use it: If they use the feature, the code is potentially not future-proof: If they have to replace the library for some reason, their code might not be compatible to other implementations of the standard that will not have the added feature. This is a form of technical debt.
  • Other code might expect an implementation to match the standard exactly and fail to compile, for example when using TypeScript. In this case, the library could not be used.
  • Now that AbortController exists, the library has to maintain two ways of aborting requests, which means more maintenance work and more potential bugs.

By discouraging the use of this non-standard feature, less code will break when the authors will eventually deprecate and remove the feature.

As in the comments of your code timeout is just a node-fetch extension. AbortSignal is the new (future?) standard way to cancel and the only cross platform way that cancelling fetch

Related