loop through async javascript -4 1

loop through async javascript -4

const j = 10;
for (let i = 0; i < j; i++) {
    asynchronousProcess(function() {
        console.log(i);
    });
}

Here is what the above code is Doing:
1. The for loop is executed 10 times.
2. Each time the for loop is executed, an asynchronous process is started.
3. The asynchronous process takes a callback function as an argument.
4. The callback function is executed after the asynchronous process is completed.
5. The callback function prints the value of i.

The problem is that the value of i is not what you expect it to be.

The reason is that the for loop has already finished executing by the time the asynchronous process is completed.

The value of i is 10 when the asynchronous process is completed.

The value of i is 10 for all 10 asynchronous processes.

The value of i is 10 when the callback function is executed.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10 for all 10 callback functions.

The value of i is 10 when the callback function prints the value of i.

The value of i is 10

Similar Posts