Skip to content

Instantly share code, notes, and snippets.

@americanstone
Created July 23, 2017 01:19
Show Gist options
  • Select an option

  • Save americanstone/dfa7eda1690d7b3835ddf09a9e606aee to your computer and use it in GitHub Desktop.

Select an option

Save americanstone/dfa7eda1690d7b3835ddf09a9e606aee to your computer and use it in GitHub Desktop.
closure
(function(){
var sequence = Promise.resolve();
for(var i = 0; i < 10 ; i++){
const timeout = Math.random() * 1000;
sequence = sequence.then((function(icopy){
return new Promise((resolve, reject) => {setTimeout(() => { console.log(icopy); resolve();}, timeout);});
})(i));
}
})()
(function(){
var sequence = Promise.resolve();
[1,2,3,4,5,6].forEach(i => {
const timeout = Math.random() * 1000;
sequence = sequence.then((function(icopy){
new Promise( (resolve, reject) => { console.log(icopy); resolve();});
})(i));
});
})()
(function(){
[1,2,3,4,5,6].forEach(i => {
const timeout = Math.random() * 1000;
setTimeout(() => console.log(i), timeout)
});
console.log("nihao");
})()
(function(){
[1,2,3,4,5,6].forEach(function(i){
const timeout = Math.random() * 1000;
setTimeout(function(){console.log(i)}, timeout)
});
})()
(function(){
for(const i = 0; i < 10 ; i++){
const timeout = Math.random() * 1000;
setTimeout(() => console.log(i), timeout);
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment