🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

fetch-timeout

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch-timeout

HTTP/S fetch wrapper that adds the possibility to set a timeout after which a custom error is returned.

0.0.2
latest
Source
npm
Version published
Weekly downloads
238
64.14%
Maintainers
1
Weekly downloads
 
Created
Source

npm version Build Status

fetch-timeout

HTTP/S fetch wrapper that adds the possibility to set a timeout after which a custom error is returned. If used in NodeJS, this package is dependent on node-fetch, altough it will always try to use window.fetch.

Installation

npm install --save fetch-timeout

Nodejs environment only

Also add the following package, since the standard window.fetch isn't accessible from node.

npm install --save node-fetch

Usage

ES5

  var fetchTimeout = require('fetch-timeout');

  fetchTimeout('https://api.github.com/', {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
  }, 5000, 'My custom timeout error string')
  .then(function(res) {
    if (res.status !== 200) {
      throw new Error('Status code not OK', res.status);
    } else {
      return res.json();
    }
  })
  .then(function(json) {
    console.log("json returned from response");
  })
  .catch(function(err) {
      console.log("error", err);
  });

ES6

  import fetchTimeout from 'fetch-timeout';

  fetchTimeout('https://api.github.com/', {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
  }, 5000, 'My custom timeout error string')
  .then(res => {
    if (res.status !== 200) {
      throw new Error('Status code not OK', res.status);
    } else {
      return res.json();
    }
  })
  .then(json => {
    console.log("json returned from response");
  })
  .catch(err => {
      console.log("error", err);
  });

API

ArgumentsTypeOptionalDefaultDescription
urlstringfalseurl to pass to node-fetch
optionsobjecttrue{}standard options to pass to node-fetch
timeoutnumbertrue10000maximum acceptable timeout before throwing the timeout error
errorstringtrue'Timeout error'custom error string after the timeout is expired

Tests

npm test

Contributing

Pull requests and suggestions are more than welcome!

Keywords

fetch

FAQs

Package last updated on 09 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts