Created
February 6, 2019 14:55
-
-
Save PinkiNice/d82079405ea8da8fd654a977a9dcdc25 to your computer and use it in GitHub Desktop.
Wrapper for sending requests 1 by 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
@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