Question: I have a problem with .sh script that saves files to zip cyclically. Another program in java tries to read these files and sometimes there is a problem because the sh script does its job and writes files into zip, is it possible to check in java whether the zip file is being written by .sh script?

EDIT: firs, thanks for all advices, I got idea: i can copy existing zip and create new one with diffrient name than the last one f.e temporary, after changes are done, i would change it name and delete old zip. Names are dates

Answer: 
Because `var` is hoisted. When you define a `var` in the `for` loop, the variable is defined at the top of the `for` loop.

```
var i = undefined;
for(i = 1; i <= 5; i++) { 
  setTimeout(function(){ 
    console.log(i) 
    }, 1000); 
}

```

The callback function of the `setTimeout` forms a closure with variable `i`. The closures are resolved when the callback is executed. Since each `for` iteration registered a callback that will be evaluated after the `for` loop exits, the value of `i` is `6` at that point.

You should use `let` instead of `var` which is block-scoped and doesn't hoist. Every iteration of `for` receives has its own `i` which will form the closure with the callback. With this, you will receive the desired result.

```
for(let i = 1; i <= 5; i++) { 
  setTimeout(function(){ 
    console.log(i) 
    }, 1000); 
}

1
2
3
4
5

```

Event loop in a nutshell
------------------------

The event loop is a piece of software in the browser (or in Node) that runs continuously in the background. Its job is to provide callback functions to the JavaScript engine (such as V8 in Chrome) to execute. The `setTimeout` function is a Web-API provided by the browser that sends the callback function to the (event-queue) task-queue to be executed inside the JavaScript engine.

The event loop picks a callback from the task-queue and pushes it on-to the call stack of the JavaScript engine, but it can only do that when the call-stack is empty. Each `for` loop iteration pushes `setTimeout` call to the call-stack where it gets executed. Hence unless all for-loop iterations are done, the call-stack is never empty. Therefore, all callbacks enqueued in the task-queue get executed in the end. It really doesn't matter what time you choose for the `setTimeout` call.

> 
> I have written this [Medium article](https://itnext.io/javascript-promises-and-async-await-as-fast-as-possible-d7c8c8ff0abc) where I've discussed the event loop in detail.
> 
> 
> 

Links
* Hoisting: <https://developer.mozilla.org/en-US/docs/Glossary/Hoisting>
* Closures: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures>
* let: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let>
One alternative to avoid using `let` and sticking to `var` if you need compatibility with stuff that doesn't use ES6.

Enclosing the scope with function separates the variables and avoids automatic hoisting with already existing once since we no longer have access to the `i` variable from inside the function.

```
for (var i = 1; i <= 5; i++) {
    // Scope enclosure
    var f = function () {
        return function (x) {
            setTimeout(function () {
                console.log(x)
            }, 1000);
        }
    };
    f()(i);
}

```