I want to pass each item into a function that take times. But seems that the JS function is asynchronized. How can I call the function sequentially ? (Pass next item to function after the previous done)
function main() {
for (var i = 0; i < n ; i++) {
doSomething(myArray[i]);
}
}
function doSomething(item) {
// do something take time
}
My solution is call the function recusively. But I want to know is there a different way to solve this issue ? Thanks.
function main() {
doSomething(myArray, 0);
}
function doSomething(item, i) {
// do something take time
doSomething(myArray, i + 1);
}
myArray.forEach(doSomething)comes to mind.