Last active
June 16, 2020 10:48
-
-
Save ZiTAL/bc4b0fd83a197dce94d2b0973bc45262 to your computer and use it in GitHub Desktop.
js: Promise loop timeout
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
var p = new Promise(function(resolve, reject) | |
{ | |
resolve(); | |
}); | |
for(var r='', i=0; i<3; i++) | |
{ | |
(function(_i) | |
{ | |
p = p.then(function() | |
{ | |
return new Promise(function(resolve, reject) { | |
var timeout = (_i===0)?0:1000; | |
setTimeout(function() | |
{ | |
console.log(_i); | |
r += _i+" "; | |
resolve(r); | |
}, timeout); | |
}); | |
}); | |
})(i); | |
} | |
p.then(function(response) | |
{ | |
console.log(response); | |
}); | |
/* | |
output: | |
0 | |
1 // 1 sencod later from init | |
2 // 2 sencods later from init | |
0 1 2 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment