JavaScript asynchronous Programming: Specific methods of asynchronous data collection _javascript tips

Source: Internet
Author: User
Tags readfile repetition

Asyncjs/seriesbyhand.js

Copy Code code as follows:

var fs = require (' FS ');
Process.chdir (' recipes '); Change Working directory
var concatenation = ';

Fs.readdir ('. ', function (err, filenames) {
if (err) throw err;

function Readfileat (i) {
var filename = filenames[i];
Fs.stat (filename, function (err, stats) {
if (err) throw err;
if (! stats.isfile ()) return readfileat (i + 1);

Fs.readfile (filename, ' UTF8 ', function (err, text) {
if (err) throw err;
concatenation + = text;
if (i + 1 = = Filenames.length) {
All files have been read to display output
return Console.log (concatenation);
}
Readfileat (i + 1);
});
});
}
Readfileat (0);
});

As you can see, the asynchronous version of the code is much more than the synchronized version. If you use the filter, foreach synchronization methods, the code is about half the number of lines, and it is much easier to read. How nice it would be if these nice iterators had an asynchronous version! You can do this with async.js!

When will it be OK to throw?

As you may have noticed, in the previous code example, the author ignores the suggestion I made in section 1.4: Throwing an exception from a callback is a bad design, especially in a finished product environment. However, a simple example of throwing an exception directly is no problem at all. If you do encounter unexpected cases of code errors, throw shuts down the code and provides a beautiful stack trajectory to explain the cause of the error.

The real flaw here is that the same error-handling logic (i.e., err) throw err) repeats as many as 3 times! In 4.2.2, we'll see how async.js helps reduce this repetition.

The functional formula of Async.js
We want to replace the filter and foreach method used by the synchronization iterator with the corresponding asynchronous method. Async.js gave us two choices.

Async.filter and Async.foreach, which process the given array in parallel.
Async.filterseries and Async.foreachseries, which process the given array sequentially.
Running these asynchronous operations in parallel should be faster, so why use a sequential approach? There are two reasons.

The problem of unpredictable workflow order mentioned earlier. We can actually store the result in a number of groups and then joining (join) the array to solve the problem, but there is one more step.
There is an upper limit on the number of files that node and any other application processes can read at the same time. If this limit is exceeded, the operating system will complain. If you can read files sequentially, you do not need to worry about this limitation.
So let's get to the async.foreachseries first. The following uses the Async.js data collection method to directly overwrite the synchronization version of the code implementation.

Asyncjs/foreachseries.js

Copy Code code as follows:

var async = require (' async ');
var fs = require (' FS ');
Process.chdir (' recipes ');//Change Working directory
var concatenation = ';

var dircontents = Fs.readdirsync ('. ');

Async.filter (dircontents, isfilename, function (filenames) {
  async.foreachseries (filenames, Readandconcat, OnComplete);
});

Function isfilename (filename, callback) {
  fs.stat (filename, function (err, stats) {
    if (err) throw err;
    Callback (Stats.isfile ());
 });
}

Function readandconcat (filename, callback) {
  fs.readfile (filename, ' UTF8 ', function (err, filecontents) {
    if (err) return callback (ERR);
    concatenation + = filecontents;
    callback ();
 });
}

Function OnComplete (err) {
  if (err) throw err;
  Console.log (concatenation);
}

Now our code is beautifully divided into two parts: a task profile (represented by Async.filter calls and async.foreachseries invocations) and implementation details (represented by two iterator functions and a complete callback oncomplete).

Filter and foreach are not the only Async.js tool functions corresponding to the standard functional iterative method. Async.js also provides the following methods:

Reject/rejectseries, and filter is just the opposite;
Map/mapseries,1:1 transform;
Reduce/reduceright, the gradual transformation of values;
Detect/detectseries, find the value of the filter match;
SortBy, producing an ordered copy;
Some, tests whether at least one value conforms to a given standard;
Every, tests whether all values meet the given criteria.
These methods are the essence of async.js, enabling you to perform common iterative work with minimal code repetition. Before continuing to explore more advanced methods, let's look at the error-handling techniques of these methods.

Error handling Technology of Async.js
Blame node of the fs.exists first open this precedent! This also means that an iterator using the Async.js data collection method (Filter/filterseries, Reject/rejectseries, detect/detectseries, some, every, and so on) cannot report an error.

For all async.js iterators of a non-Boolean type, passing a null/undefined value as the first parameter of the iterator callback will immediately invoke the completion callback because of the error value. That's why Readandconcat can work without throw.

Asyncjs/foreachseries.js

Copy Code code as follows:

function readandconcat (filename, callback) {
Fs.readfile (filename, ' UTF8 ', function (err, filecontents) {
if (ERR) return callback (ERR);
concatenation + = FileContents;
Callback ();
});
}

So if callback (ERR) is actually called in Readandconcat, this err is passed to the completion callback (that is, oncomplete). Async.js is only responsible for ensuring that OnComplete is invoked only once, whether it is invoked for the first error, or because all operations have been successfully completed.

Asyncjs/foreachseries.js

Copy Code code as follows:

function OnComplete (err) {
if (err) throw err;
Console.log (concatenation);
}

Node's error-handling conventions may not be ideal for async.js data-gathering methods, but for all other methods of async.js, complying with these conventions can make the error cleanly flow from each task to the completion callback. More examples are shown in the next section.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.