Created
November 8, 2016 03:44
-
-
Save gyandeeps/4672114a02d49e1e7a1745a79da64070 to your computer and use it in GitHub Desktop.
Keep calling a promise every n sec until resolved
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
let i = 0; | |
const test = () => new Promise((resolve, reject) => { | |
i++; | |
console.log(`Promise called - ${i}`); | |
if (i === 5) { | |
resolve(); | |
} | |
else { | |
reject(); | |
} | |
}); | |
const repeat = () => new Promise((resolve, reject) => { | |
let interval = setInterval(() => { | |
test().then(() => { | |
clearInterval(interval); | |
resolve(); | |
}); | |
}, 500); | |
}); | |
repeat() | |
.then(() => console.log("pass")) | |
.catch(() => console.log("error")); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment