Asynchronous loop without recursion? [closed]

2020-05-09 23:20发布

Is it possible to implement an asynchronous loop in JavaScript without recursion?

Case in point, is it possible to synchronize a queue of asynchronous tasks without resorting to recursion? We have N asynchronous tasks in a queue, and need to execute each task after the previous one has finished.

All the examples I've seen so far use recursion for this. But if we have to deal with a very long queue, we cannot expect anything good from a recursive approach. So what's the alternative then, how to tackle this?

Just so, when a similar question was asked about promises, every single answer relies on recursion.

1条回答
老娘就宠你
2楼-- · 2020-05-09 23:26

we cannot expect anything good from a recursive approach.

Your premise is wrong. As you are doing it asynchronous, there is nothing wrong with a recursive approach (sometimes dubbed pseudo-recursion), it doesn't grow the call stack.

查看更多
登录 后发表回答