@altipla/fetch-timeout
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

fetch-timeout

Implement timeout and cancellation on top of node-fetch or standard fetch.

Install

npm install fetch-timeout

Usage

Replace any call to fetch with our helper fetchTimeout. Types are compatible as we only add a new option timeout that can be added when needed.

With no timeout (normal fetch)

let reply = await fetch('https://www.example.com/', {
  method: 'POST',
  body: '...',
  headers: { ... },
})

With a timeout

let reply = await fetch('https://www.example.com/', {
  method: 'POST',
  body: '...',
  headers: { ... },
  timeout: 30000, // milliseconds
})

With a timeout and a cancel signal combined

let ctrl = new AbortController()

abortButton.addEventListener('click', () => ctrl.abort())

let reply = await fetch('https://www.example.com/', {
  method: 'POST',
  body: '...',
  headers: { ... },
  timeout: 30000,
  signal: controller.signal,
})

Check if there was a timeout

try {
  let reply = await fetch('https://www.example.com/', {
    timeout: 5000,
  })
} catch (err: any) {
  if (err.name === 'AbortError') {
    ...
  }
}

/@altipla/fetch-timeout/

    Package Sidebar

    Install

    npm i @altipla/fetch-timeout

    Weekly Downloads

    3

    Version

    0.1.4

    License

    MIT

    Unpacked Size

    6.17 kB

    Total Files

    7

    Last publish

    Collaborators

    • ernestoalejo