Created
June 18, 2014 15:58
-
-
Save jnicklas/8335ce5058f8d40b15ce to your computer and use it in GitHub Desktop.
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
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