Skip to content

Instantly share code, notes, and snippets.

@PinkiNice
Created February 6, 2019 14:55
Show Gist options
  • Save PinkiNice/d82079405ea8da8fd654a977a9dcdc25 to your computer and use it in GitHub Desktop.
Save PinkiNice/d82079405ea8da8fd654a977a9dcdc25 to your computer and use it in GitHub Desktop.
Wrapper for sending requests 1 by 1
/*
@Params:
action: Promise
*/
const syncRequests = function(action) {
let req = null;
let requestInProcess = false;
return (...params) => {
if (requestInProcess) {
return req;
}
req = action(...params);
requestInProcess = true;
req.finally(() => {
requestInProcess = false;
});
return req;
};
};
const fetchData = () => axios.get('/posts');
const syncFetchData = syncRequests(fetchData);
setInterval(() => {
syncFetchData();
}, 1)
/*
Requests to network will only land 1 by 1.
So there is never will be 2 parallel network requests hanging.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment