Trying to print a series of numbers inside a loop using closures and with let:
Consider the following example:
for(var i=1; i<10; i++){
setTimeout(function(){
document.write(i);
}, 1000);
}
Output:
10101010101010101010
With closing:
for(var i=1; i<10; i++){
(function(x){
setTimeout(function(){
document.write(x);
}, 1000);
})(i);
}
Output:
123456789
Without closing, just using ES6 let:
for(let i=1; i<10; i++){
setTimeout(function(){
document.write(i);
}, 1000);
}
Output:
123456789
Trying to figure out if locks are still needed using IIFE blocks moving to ES6?
Any good example if we really need ES6 closures?
source
share