In your recursive function, you can do this:
... if (idx < someArray.length) { doAsync(function() { resolve(doRecursion(idx + 1)); }); } else { ...
In other words, although idx less than someArray.length , your promise will resolve another promise, this time the promise is returned by calling doRecursion() with idx incremented by one. then lower callback will not be called until doRecursion resolves any value other than the promise. In this case, it will eventually resolve with the value 'done!' .
However, if you use promises, you probably won't need to use recursion at all. You may need to reorganize your code a bit more, but I would suggest considering the @BenFortune option as an alternative.
source share