Created
June 21, 2015 04:45
-
-
Save LeoOnTheEarth/a4e87121d96835e6e0f6 to your computer and use it in GitHub Desktop.
Promise timeout example
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
// 這個範例可以讓 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