Skip to content

Instantly share code, notes, and snippets.

@leonidkuznetsov18
Created April 29, 2021 15:28
Show Gist options
  • Select an option

  • Save leonidkuznetsov18/22ff2215d94d76c9c2d54c0e80246d0c to your computer and use it in GitHub Desktop.

Select an option

Save leonidkuznetsov18/22ff2215d94d76c9c2d54c0e80246d0c to your computer and use it in GitHub Desktop.
continuously fetching data
const getData = () =>
new Promise((resolve) =>
setTimeout(() => resolve(Math.random() * 5), Math.random() * 2000)
);
const fetchThrottledData = () => {
const arrOfData = [];
let startTime = new Date().getTime();
const res = () => {
let interval = setInterval(async () => {
if (new Date().getTime() - startTime > 60000) {
clearInterval(interval);
return;
}
try {
const data = await getData();
arrOfData.push(data);
console.log("arrOfData", arrOfData);
} catch (error) {
console.error(error);
} finally {
fetchThrottledData();
}
}, 1000);
console.log("startTime", startTime);
console.log("interval", interval);
}
return res;
};
fetchThrottledData()();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment