Created
April 29, 2021 15:28
-
-
Save leonidkuznetsov18/22ff2215d94d76c9c2d54c0e80246d0c to your computer and use it in GitHub Desktop.
continuously fetching data
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
| 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