Skip to content

Instantly share code, notes, and snippets.

@gyandeeps
Created November 8, 2016 03:44
Show Gist options
  • Save gyandeeps/4672114a02d49e1e7a1745a79da64070 to your computer and use it in GitHub Desktop.
Save gyandeeps/4672114a02d49e1e7a1745a79da64070 to your computer and use it in GitHub Desktop.
Keep calling a promise every n sec until resolved
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