Question: Having the code:

```
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="script2.js"></script>

```

script.js:

```
setTimeout(function(){
    console.log("first");
}, 15);

```

script2.js:

```
setTimeout(function(){
    console.log("second");
}, 10);

```

while running it I got different results. One time the output is:

```
second
first

```

but another time:

```
first
second

```

Shouldn't it be always in the "second, first" order due to given time delay?

Answer: 
You have a simple race condition.

The spec guarantees that the script files run in order. That means that the browser is free to attempt to download the files at the same time, but even if script2.js loads before script.js, script.js will run first.

However, if script.js loads quickly and is executed while script2.js is still downloading from the server, there is a chance that at least 5ms will pass before script2.js is executed. If that happens, the timeout in script.js will be executed first, and the output will be `first, second`.
I have three way to solve your question.

1. Call first function in second function.

```
function first(){
    second();
    setTimeout(function(){
        console.log("first");
    }, 10);
}

function second(){
    setTimeout(function(){
        console.log("second");
    }, 10);
}

```
2. Just give a more interval to First function.

```
function first(){
    setTimeout(function(){
        console.log("first");
    }, 11);
}

function second(){
    setTimeout(function(){
        console.log("second");
    }, 10);
}

```
3. Use Flag.

```
var flag = false; //global variable

function first(){
    setTimeout(function(){
        if(flag){
            console.log("first");
        }else{
            first();
        }
    }, 10);
}

function second(){
    flag = true;
    setTimeout(function(){
        console.log("second");
    }, 10);
}

```