Skip to content

Instantly share code, notes, and snippets.

@capJavert
Last active October 4, 2019 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capJavert/e0ae5b2dc989263d57a5c82450b09c90 to your computer and use it in GitHub Desktop.
Save capJavert/e0ae5b2dc989263d57a5c82450b09c90 to your computer and use it in GitHub Desktop.
Simple fetch retry polyfill.
/**
* Simple polyfill to support retry of fetch requests
*/
;(() => {
global.fetchOriginal = global.fetch
/**
* Create retry fetch with last request arguments
*
* @param {string} resource request URL or resource to fetch
* @param {object} options options for request
* @return {Function} new fetch method
*/
global.fetchRetry = (resource, options = {}) =>
/**
* Fetch method with preloaded request arguments
* @param {[type]} newOptions Override options of last request
* @return {Promise}
*/
(newOptions = {}) => {
const mergedOptions = {
...options,
...newOptions,
// reset signal option from previous fetch
signal: newOptions.signal || undefined
}
return global.fetchOriginal(resource, mergedOptions)
}
/**
* Override fetch and save last request arguments
*
* @param {string} resource request URL or resource to fetch
* @param {object} options options for request
* @return {Promise}
*/
global.fetch = async (resource, options) => {
const response = await global.fetchOriginal(resource, options)
response.fetchRetry = global.fetchRetry(resource, options)
return response
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment