I just decided to brush up on my core Node.js skills. Simple servers, reading streams, pipes etc. This quickly leads to the problem of asynchronicity. In the spirit of deepening my understanding of async, and trying to relate my experience and how I though about it, I submit the following…

When multiple asynchronous operations are being carried out at the same time, they won’t always return in the same order in which they began. Often, we need to wait for all of these operations to complete, and then deal with their returns in the original order. I found counting is one way to accomplish this.

Lets frame our steps in an easy to understand story:

Imagine a theme park. All visitors enter in a line, the visitors go enjoy the theme park for as long as they need to, but no visitors can leave until they are all done, and they must exit in the same order they entered.

How can this be accomplished?

1 . First, we need a way to order our visitors.

Let’s give them a number.

2. Next we need a way to know when all visitors have finished in the park.

Here, we can start by taking a total count of all visitors. As they finish, they can tell us they’re done and we can decrement our count. When we have a visitor count of 0, we know everybody can leave.

3. Finally, we need to return them in the same order they came in.

We have visitors, with numbers. *hint hint* (Sounds like array values)