Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




The async map function

Node is non-blocking and will continue on to perform subsequent operations, even if it executes an operation that may take significant time to complete. Unless subsequent operations depend on the output of prior operations, Node executes asynchronously.

Blocking is when the execution of subsequent code must wait until a non-JavaScript operation completes. (For operations written in JavaScript, the event loop is unable to continue running the code while a blocking operation is occurring. Node must handle these operations synchronously.)

From the Node documentation an example of blocking (synchronous) file read is:

And an example of non-blocking (asynchronous) file read is:

With this, the async map function or service is a way of dealing with asynchronous JavaScript. We may have situations where we are not in control of which operations execute fully first, but are still in need of returning the results of our mapping function in the same order as our inputs. For example suppose we have an array of ids and a callback function that fetches the item names using each id. It would be confusing and of little use if we received the names back in the (unknown) order in which they are fetched rather than in the same order of our input.

Say we have the following array of engines and an array of ids:

Our fetch function to get the names of the items using the list of id numbers above is as follows:

We can use this function as we map through the array of ids to return us the name associated with that id. But the name of the item associated with id 4 may be returned before that of 1. How do we then keep control over the order in which the names are returned to us?

The idea is to have a data structure with an ordering mechanism such as an array (because it has numbered indices) in which we can slot our results at a predetermined index as they come.

In this way, we can use the async map function to loop through our idList array, apply our getNameFromStorage function with each id and store the retrieved values in our array to be returned to us in the order we expect.

The asyncMap function takes the array of ids (first input) and iterates through them, applying the name retrieval function (second input) to get the name of the item given the id. At the very end, it invokes the callback function (third input) to return the data structure that we used to store our results in the order we expect.

Invoking the function as follows will return our results data structure.

The above illustration will handle the asynchronous nature of JavaScript but our example does not demonstrate the point fully yet. If we delay the return of each name by applying a setTimeout of random time between 0 and 1000ms to each fetch operation, we can demonstrate that we can still return the names in the expected order, despite receiving the names back in a potentially different and unknown order. By inspecting the console log that prints out the random delay time as well as the returned name, one can see the exact order in which the values are retrieved. This may not be in the ordered fashion we desire. However, our async function slots them into our results array in the desired and ordered fashion, and it is this results array that we will use.

This technique can be applied to the reading of external files where one has an identifier of those files but because of space constraints, those files are not stored internally. An external API call must be made to retrieve the data required given the list of identifiers. Async map will return us the data in an order we know and can work with.

Add a comment

Related posts:

Learning how to learn

Have you ever faced difficulty while learning something or a situation when you can’t figure out what to do and how to do? Well everybody at some point in the lives has faced such situations where…

A Nation of Snowflakes

The American left is waging war on free speech. That’s the consensus from center-left to far right; even Nazis and white supremacists seek to wave the First Amendment like a bloody shirt. But the…

Yarimar Bonilla

Welcome to the publication, “Representations.” This is a project designed to bring the perspectives of a wider variety of groups to the forefront of the anthropology classroom. In recognition of…