Skip to content

Instantly share code, notes, and snippets.

@hashtafak
Created August 12, 2020 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashtafak/90325d0124bd0e17c009d61e2bf40ff6 to your computer and use it in GitHub Desktop.
Save hashtafak/90325d0124bd0e17c009d61e2bf40ff6 to your computer and use it in GitHub Desktop.
setInterval stop duplicate fetch response
// maybe not a good case for http-pooling but works in some case of pet project
let smokeBreak = false; // https://www.youtube.com/watch?v=kdRPFBJ-jVc
try {
const x = setInterval(() => {
if (!smokeBreak) {
smokeBreak = true;
fetch('// a long wait request maybe wait more 5 sec to read: network error ')
.then((res) => res.json())
.then((job) => {
// your logic code
if (job.success) {
clearInterval(x);
console.log(job);
} else {
smokeBreak = false;
}
})
}
}, 5000);
} catch (err) {
console.log(err.message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment