Created
August 12, 2020 18:15
-
-
Save hashtafak/90325d0124bd0e17c009d61e2bf40ff6 to your computer and use it in GitHub Desktop.
setInterval stop duplicate fetch response
This file contains 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
// 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