I hope this will make sense:
I need to create a foreach function in javascript that will be used like this:
foreach(["A", "B", "C"], function(letter, done) {
// do something async with 'letter'
doSomthing(letter, done); // ***
}, function () {
// final callback that is called after all array has been visted.
// do some final work
});
So I was thinking about the following implementation:
var foreach = function(array, func, ready) {
if (!array.length)
ready();
var that = this;
func(array[0], function(){
that.foreach(array.slice(1, array.length), func, ready);
});
}
And it seems that it actually works! very cool.
But I was thinking if there is a solution that doesn’t use recursion?
I couldn’t think of one…
Your approach is technically correct but it is not good to do in such a way.
Pls implement using promise pattern in javasript .
I recommend you using when.js an open source js available on git for implementing promise pattern Pls refert to the below code