Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Last active June 16, 2020 10:48
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 ZiTAL/bc4b0fd83a197dce94d2b0973bc45262 to your computer and use it in GitHub Desktop.
Save ZiTAL/bc4b0fd83a197dce94d2b0973bc45262 to your computer and use it in GitHub Desktop.
js: Promise loop timeout
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