array and asynchronous function callback
javascript
Solution
var arrayFunc = function(array) {
if (array.length > 0) {
func(array[0], function() { arrayFunc(array.slice(1)); });
}
}
This will run your function with the first element in the array, and then have the continuation function take the rest of the array. So when it runs it will run the new first element in the array.
EDIT: here's a modified version that doesn't copy the array around:
var arrayFunc = function(array, index) {
if (index < array.length) {
func(array[index], function() {
var newI = index + 1;
arrayFunc(array, newI);
});
}
}
And just call it the first time with an index of 0.
Problem
I got async function: ``` var func = function (arg, next) { var milliseconds = 1000; setTimeout(function(){ console.log (arg); next() } , milliseconds); } ``` And array: ``` var arr = new Array(); arr.push (0); arr.push (1); console.log(arr); ``` I want to use `func` for every item of my array `arr`: ``` func(arr[0], function(){ func(arr[1], function(){ console.log("finish"); }) }) ``` Ok for array consisted of 2 elements, but if I got array of 1000 elements how to use `func` for every item in `arr`? How to do it in cycle?