Skip to content

Instantly share code, notes, and snippets.

@LeoOnTheEarth
Created June 21, 2015 04:45
Show Gist options
  • Save LeoOnTheEarth/a4e87121d96835e6e0f6 to your computer and use it in GitHub Desktop.
Save LeoOnTheEarth/a4e87121d96835e6e0f6 to your computer and use it in GitHub Desktop.
Promise timeout example
// 這個範例可以讓 N 個 timeout 循序地執行
var count = 0;
var n = 100;
function timeoutPromise()
{
return new Promise(function(resolve) {
setTimeout(
function() {
console.log(++count);
resolve('kukuku');
},
1000
);
});
}
var p = timeoutPromise();
for (var i = 1; i < n; ++i) {
p = p.then(function() {
return timeoutPromise();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment