Three implementation methods for synchronous operations in node. js, node. js3

Source: Internet
Author: User

Three implementation methods for synchronous operations in node. js, node. js3

As we all know, Asynchronization is a unique feature and advantage, but it is also common to require synchronization in the Program (for example, the execution sequence of the control program is func1-> func2-> func3. This article records some of your thoughts on this issue.

Functions to be executed:

Copy codeThe Code is as follows:
Var func1 = function (req, res, callback ){
SetTimeout (function (){
Console. log ('in func1 ');
Callback (req, res, 1 );
},13000 );
}
Var func2 = function (req, res, callback ){
SetTimeout (function (){
Console. log ('in func2 ');
Callback (req, res, 2 );
},5000 );
}

Var func3 = function (req, res, callback ){
SetTimeout (function (){
Console. log ('in func3 ');
Callback (req, res, 3 );
},1000 );
}

It can be seen that the setTimeout function is used in func1, func2, and func3, And the execution time is 13 seconds, 5 seconds, and 1 second respectively. Due to the asynchronous feature of nodejs, if you use a common function call method:

Copy codeThe Code is as follows:
Var req = null;
Var res = null;
Var callback = function (){};
Func1 (req, res, callback );
Func2 (req, res, callback );
Func3 (req, res, callback );

Output content:

Copy codeThe Code is as follows:
In func3
In func2
In func1

The reason is that nodejs is asynchronous, and func2 will not execute it after the execution of func1 is completed, but will execute it immediately (as does func3 ). Func3 ended first because of the shortest running time, followed by func2 and last by func1. But this is obviously not the result we want. What should I do?

Solution 1: callback

Copy codeThe Code is as follows:
// Deep nesting
Var req = null;
Var res = null;

Func1 (req, res, function (){
Func2 (req, res, function (){
Func3 (req, res, function (){
Process. exit (0 );
})
});
});

Although this method can be quickly solved, the problems exposed are also obvious. One is that the Code is not maintained, and the other is that the deep nesting of the code looks uncomfortable. This method is not desirable.

Solution 2: recursive call

Copy codeThe Code is as follows:
Function executeFunc (funcs, count, sum, req, res ){
If (count = sum ){
Return;
}
Else {
Funcs [count] (req, req, function (){
Count ++;
ExecuteFunc (funcs, count, sum, req, res );
});
}
}

// Synchronous call
Var req = null;
Var res = null;
Var funcs = [func1, func2, func3];
Var len = funcs. length;
ExecuteFunc (funcs, 0, len, req, res );

First, multiple functions are grouped into an array. Then, we can use the features of recursive functions to make the program run in a certain order.

Solution 3: Call the class library

With the development of nodejs, more and more class libraries are responding. Step and async are good.

1. Step calls are relatively refreshing:
Copy codeThe Code is as follows:
Step (
Function thefunc1 (){
Func1 (this );
},
Function thefunc2 (finishFlag ){
Console. log (finishFlag );
Func2 (this );
},
Function thefunc3 (finishFlag ){
Console. log (finishFlag );
}
);

2. The series method of async. In this example, its call method is as follows:
Copy codeThe Code is as follows:
Var req = null;
Var res = null;
Var callback = function (){};

Async. series (
[
Function (callback ){
Func1 (req, res, callback );
},
Function (callback ){
Func2 (req, res, callback );
},
Function (callback ){
Func3 (req, res, callback );
}
]
);

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.