1.0.0 • Published 6 years ago

fetch-fetch-fetch v1.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

fetch-fetch-fetch

The Fetch API is a api that provides for interacting with network resources in a asynchronous manner. As of this writing, the native implementation is well supported in all browsers except IE. For full legacy support it is best to use a polyfill, such as whatwg-fetch.

This project wraps whatever version of the Fetch API you've chosen to use (native or polyfilled), and adds mechanisms that allow for specifying timeout length and retries.

Read this first

  • This library does not provide any implementation of the Fetch API and, as such, is not responsible for retrieving data. If you have found an error or bug in data retrieval, please open a bug with the relevant polyfill or browser team.

Installation

  • 'yarn add fetch-fetch-fetch'; or
  • 'npm install fetch-fetch-fetch --save'

Import the wrapper function:

import 'fetchFetchFetch' from 'fetch-fetch-fetch';

Usage

The first two parameters to the function are just the standard Fetch API parameters, consisting of the url and the init parameters, usage here.

Specifying a timeout length

A timeout length can be passed in milliseconds to the wrapper api. If a response is not received within the specified time period, the call is considered failed and an Error is thrown.

fetchFetchFetch('/my/data', undefined, 5000, undefined)
    .then(response => { console.log(response); })
    .catch(error => { console.log('Call failed'); });

Retrying failed calls

The wrapper api allows for specifying the number of times that the call should be tried. Retries will occur only if the call times out. A failure response from the service is considered a valid response and will be returned as normal.

fetchFetchFetch('/my/data', undefined, 5000, 2)
    .then(response => { console.log(response); });