loop through async javascript -1 1

loop through async javascript -1

someArray.forEach(function(item, i) {
    asynchronousProcess(function(item) {

Here is what the above code is Doing:
1. The forEach method is called on the someArray array.
2. The forEach method takes a callback function as an argument.
3. The callback function takes two arguments: item and i.
4. The asynchronousProcess function is called, and the callback function is passed as an argument.
5. The asynchronousProcess function calls the callback function.
6. The callback function logs the value of i to the console.

The problem with the above code is that the value of i is logged as 3 three times. This is because the forEach method has already finished looping through the array by the time the asynchronousProcess function calls the callback function.

The solution to this problem is to use the let keyword.

Similar Posts