Skip to content

Instantly share code, notes, and snippets.

@jnicklas
Created June 18, 2014 15:58
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 jnicklas/8335ce5058f8d40b15ce to your computer and use it in GitHub Desktop.
Save jnicklas/8335ce5058f8d40b15ce to your computer and use it in GitHub Desktop.
function sleep(time) {
return new Promise(function(resolve) { setTimeout(resolve, time) })
}
function repeat(times, cb) {
var resolved = new Promise(function(resolve) { resolve() });
if(times === 0) {
return resolved;
} else {
return cb().then(function() {
return repeat(times - 1, cb)
});
}
}
var firstTime = true;
repeat(4, function() {
console.log("iterating!")
return sleep(200).then(function() {
if(firstTime) {
firstTime = false;
return sleep(5000);
} else {
return sleep(200);
}
})
}).then(function() {
console.log("done");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment