Skip to content

Instantly share code, notes, and snippets.

@Andy-set-studio
Created December 4, 2019 15:18
Show Gist options
  • Save Andy-set-studio/204ab4d620968190f7f6f0a6b36d6211 to your computer and use it in GitHub Desktop.
Save Andy-set-studio/204ab4d620968190f7f6f0a6b36d6211 to your computer and use it in GitHub Desktop.
Sleep fetch function
/**
* Returns a fetch request after a defined timeout
*
* @param {String} url The url that you’re fetching
* @param {Object} options={} Fetch API parameters
* @param {Number} timeout=window.sleepFetchTimeout||1000 You can completely override the timeout globally with window.sleepFetchTimeout
* @returns {Promise}
*/
const sleepFetch = (url, options = {}, timeout = window.sleepFetchTimeout || 0) => {
return new Promise(resolve => {
setTimeout(() => {
resolve(fetch(url, options));
}, timeout);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment