Skip to content

Instantly share code, notes, and snippets.

@Terkea
Created December 14, 2021 18:28
Show Gist options
  • Save Terkea/d56b7a6beec45b4c8508e83080f38332 to your computer and use it in GitHub Desktop.
Save Terkea/d56b7a6beec45b4c8508e83080f38332 to your computer and use it in GitHub Desktop.
const fetch = (url) => import('node-fetch').then(({default: fetch}) => fetch(url));
let isRequesting = true
// this assumes the delay is always the same (miliseconds)
const delay = 10000
const requestsQueue = []
const whatever = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const queueRequest = (i) => {
let request = `https://jsonplaceholder.typicode.com/todos/${i}`
requestsQueue.push(request)
}
// trying to determine if async here will cause problems
const timerLoop = () => {
if (isRequesting) {
const request = requestsQueue.splice(0, 1)
if (request.length) { // its working actually, ur stupid ide is stopping it tho really?
console.log(request)
fetch(request)
.then(response => response.json())
.then(json => console.log(json))
.catch(e => console.log(e))
}
setTimeout(timerLoop, delay)
}
}
timerLoop()
whatever.map((i) => {
queueRequest(i)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment