Skip to content

Should we deprecate timeout in favor of AbortController? #523

@jimmywarting

Description

@jimmywarting

Timeout was a node-fetch only additions in it's early days and was never implemented in the spec. Now that there is a way to control it using the AbortController to be able to control when it should abort a request. One could control whether or not a timeout should affect the hole request and response or one or the other

what would you think about removing it and instead help out on creating a abort signal?

(thinking maybe deprecate timeout in v2 or v3 - and remove it completely in v3 or v4)

To help out other on a migration path I where thinking maybe of some way to include a helper function that is optional and isn't included in the bundle so it makes it more modular.

some proposal:

const timeout = require('node-fetch/timeout')
const signal = timeout(1000)
/*
  Same as doing
  const controller = new AbortController()
  const { signal } = controller
  setTimeout(controller.abort, 1000)
*/
fetch(url, { signal })
fetch(url, { signal: timeout(1000) })

Or if it returns a object then you can use spread to append it

const timeout = require('node-fetch/timeout')
console.log( timeout(1000) ) // { signal: signal }

fetch(url, { headers, ...timeout(1000) })

Or returning a object that can help you start and stop the a singal to be able to control if the timeout should apply to only the upload part or the download part

// what node-fetch/timeout would look like
const AbortController = require('./AbortController')

module.exports = function timeout(delay) {
  const controller = new AbortController()
  const { signal } = controller
  let timer = null

  return {
    signal,
    start() {
      clearTimeout(timer)
      timer = setTimeout(controller.abort, delay)
      return signal
    }
    stop() {
      clearTimeout(timer)
    }
  }
} 
const timeout = require('node-fetch/timeout')
const timer = timeout(1000)

// for it's hole duration
fetch(url, { signal: timeout(1000).start() })

// just for upload
const timer = timeout(1000)
fetch(url, { signal: timer.start() })
  .then(res => {
    timer.stop()
  })


// just for download
const timer = timeout(1000)
fetch(url, { signal: timer.signal })
  .then(res => {
    timer.start()
  })

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions