Node. js to achieve synchronization operation of the three methods

  • 2020-03-30 04:29:27
  • OfStack

As we all know, asynchrony is a unique feature and advantage, but the requirement of synchronizing in the program (for example, the execution order of the control program is: func1 -> Func2 - > Func3) is also common. This article is a record of some of your thoughts on this issue.

Functions to be executed:


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 setTimeout function is used in func1, func2 and func3, and the execution time is 13 seconds, 5 seconds and 1 second respectively. Because of the asynchronous nature of nodejs, if you call methods using ordinary functions:


var req = null;
var res = null;
var callback = function(){};
func1(req,res,callback);
func2(req,res,callback);
func3(req,res,callback);

Output content:


in func3
in func2
in func1

The reason is that nodejs is asynchronous, func2 is not executed after func1 is executed, but immediately (as is func3). Due to the shortest running time of func3, it is the first to end, func2 is the second, and func1 is the last. But that's clearly not what we want. How to do?

Solution 1: callback


//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, but the exposed problems are also obvious, one is the code maintenance aspects, and the second is the deep nesting of the code looks very uncomfortable. This approach is not desirable.

Solution two: recursive calls


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);

Start with an array of functions. Then you can take advantage of the recursive function features, so that the program in a certain order.

Solution 3: invoke the class library

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

1. The invocation of Step is relatively clean:


Step(
  function thefunc1(){
    func1(this);
  },
  function thefunc2(finishFlag){
    console.log(finishFlag);
    func2(this);
  },
  function thefunc3(finishFlag){
    console.log(finishFlag);
  }
);

2. Async's series method. For this example, its calling method:

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 articles: