Nodejs Concurrent Asynchronous callback processing

Source: Internet
Author: User
Tags readfile

This is said to be asynchronous, not accurate, should say continuous asynchronous. The Nodejs single-threaded asynchronous feature, which directly causes multiple asynchronous simultaneous executions, cannot determine the final execution result to callback. To give a simple example:

 for (var i = 0; i < 5; i++) {    fs.readfile (function(error, data) {});}

5 consecutive asynchronous operations to read the file, very simple, then the question came, how can I be sure that all async is done? Because after they are all executed, the subsequent operation can be performed. It is a problem to believe that some experienced students will think of using the method of counting, but how to ensure that the count is correct. Think about it carefully:

The callback is a function that each asynchronous operation will counter +1, when each asynchronous end will counter-1, by judging whether the counter is zero to determine whether to execute the callback. This logic is simple, requires a global variable relative to the execution time and callback as a counter, and to be passed to the Async method is to do +1 of the operation, and then return a function for the callback, a bit around, but look at the advanced usage of the JS function:

var pending = (function() {    var count = 0;     return function () {        count++        ; return function () {            count--;             if (count = = 0)                {//  all execution completed }}}     );

When pending is called, it is pending (), such as:

var done = Pending ();

At this point the count variable count is initialized to 0, and the returned function is appended to done, what if done ()? is not directly executed pending return of the first function, namely: Pending () (), this execution is what, first the count variable count+1, and return a function, this function directly as callback to asynchronous method, when the implementation of this callback , the first is to count-1 the count variable, and then determine whether count is 0, or 0, which means that all asynchronous executions have completed, thus achieving continuous asynchronous, same callback operation.

The key is on the two return, simply say:

The first return function is to count+1, then return the function that needs the callback

The second return function is the function that needs a callback, if it executes, it will count-1, and then determine whether the asynchronous execution is complete, and the callback

To see an example of the actual point, read the asynchronous callback for multiple files:

var fileName = [' 1.html ', ' 2.html ', ' 3.html ']; var do = Pending (function(fileData) {    console.log (' done ');    Console.log (Fieldata);});  for (var i = 0; i < filename.lenght; i++) {    ' utf-8 ', Done (Filename[i]));}

The done, that is, using the pending method to wrap the method we want to callback execution, when the counter is 0 o'clock, we will execute it, then we have to improve the pending method:

varPending = (function(callback) {varCount = 0; varreturns = {};    Console.log (count); return function(key) {count++;        Console.log (count); return function(Error, data) {count--;            Console.log (count); Returns[key]=data; if(count = = 0) {callback (returns); }        }    }});

Callback is our callback function, when var done = Pending (callback), do is actually the first return function, it has a parameter that can be used as the subscript of the returned value, so in the loop body do (Filename[i]) , the file name is passed in. This done () is executed directly, and after it count+1, it returns the callback function to be passed to the Async method, as stated earlier, the callback function will determine whether to execute the callback function we wish to execute, and pass the contents of the file to it, namely returns, according to the count variable. Well, run it and believe that you can see the results of the operation accurately.

0123210done{"1.html": "xxx", "2.html": "xxx", "3.html": "XXX"}

It is evident from the count that from 0-3 to 0, then our callback function outputs the contents of the done and the file.

This problem solved, we have to think about how to let such a method encapsulation reuse, otherwise, every time write pending is not very unscientific?

Here's a look at Unjs (one of my nodejs-based Web development frameworks) that applies to child template operations in template parsing:

function (Task, Func, callback) {    var tasklen = task.length;     if (Tasklen <= 0) {        return;    }     var done = unjs.pending (callback);      for (var i = 0; i < Tasklen; i++) {        func (Task[i], done);}    }

The asyncseries has three parameters, meaning:

Task: The object that needs to be processed, such as the file that needs to be read, it is a list, if it is not a list, or the list length is 0, it will not execute

Func: An asynchronous method, such as Fs.readfile, is passed in through it.

Callback: We want the callback method

Done and the same as before, it is passed to Func, but not executed, because I want the application side can control parameters, so let the application side to execute.

And look at the action when working with the child template:

varSubtemplate = [];varPatt =/\{\% include \ ' (. +) \ ' \%\}/IG; while(Sub =patt.exec (data)) {    varSubs =Sub; Subtemplate.push ([subs[0], Subs[1]]);} Unjs.asyncseries (Subtemplate,function(item, callback) {Fs.readfile ('./template/' + item[1], ' Utf-8 ', Callback (item[0]));}, function(data) { for(varKeyinchdata) {HTML=html.replace (Key, Data[key]); }});

Subtemplate This list is the data generated from the parsing of the child template, which is a two-dimensional array, the first value of each subkey is the call text of the child template, namely: {% include ' header.html '%} such a string, the second argument is a child template filename, namely: header.html

The second parameter of Asyncseries is callback, which is actually the third parameter, which is the callback method that we want to execute pending the callback function, as mentioned above, inside the asyncseries, it is not running, but it is running here, That is: Callback (Item[0]), with parameters, because this parameter is followed by replacing the string that invokes the child template in the parent template with the contents of the corresponding child template.

This way, you can use the Asyncseries method as long as you need a continuous asynchronous process. Because of the asynchronous relationship, the process of the program is a bit around, it may start not very good understanding, even if familiar with, it is possible to suddenly do not understand, it doesn't matter, for example, the second parameter in the callback is actually the third parameter generated, beginning may you think, this callback what is inverted bottom. There is a pending two return, is not very good understanding, need to think more.

Well, successive asynchronous callbacks are done using the advanced features of the JS function. But the asynchronous nature of the Nodejs really makes the control of the program very problematic, such as the continuous asynchronous, but to pass the value of the operation, and so on, these can be through the idea of change can be achieved.

Hope this article to be Nodejs asynchronous operation to get dizzy students have a little help.

Nodejs Concurrent Asynchronous callback processing

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.