Home >Web Front-end >JS Tutorial >A simple Node.js asynchronous operation manager sharing_javascript skills

A simple Node.js asynchronous operation manager sharing_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:50:471189browse

I’ve been writing a lot about nodejs recently, and I encountered less asynchronous operations at the beginning, because what I wanted to do was relatively simple. I checked the API and found that there was a synchronous one. To save trouble, I just used the synchronous one. Gradually I found out This is not a problem. You don’t need the good asynchronous features, but you have to use synchronous ones. It’s really embarrassing, and many things don’t have synchronous APIs.

Okay! Writing asynchronously, this kind of code gradually appeared. . .

Copy code The code is as follows:

mysql.query('xxxx').on(' success', function(){
mysql.query('xxxx').on('success', function(){
mysql.query('xxxx').on('success', function() {
mysql.query('xxxx').on('success', function(){
mysql.query('xxxx').on('success', function(){
mysql. query('xxxx').on('success', function(){
                                                                                                      });
});
});
});

Well, you have also seen how ugly the code will be if it continues like this. It will look like an old lady’s footcloth, so the following asynchronous operation manager is generated, Small and exquisite, hehe, it is definitely enough. As for the code, let’s talk about it with the code. Just highlight the code, such as:
TODO: Not comprehensive enough, for example, errors are not handled


Copy code The code is as follows:

/*
* Async Manager
* author : jser.me
*
* Usage:
* var asyncMg = require('./AsyncManager') ;
* asyncMg
* .push(function(next){
* some_aysnc_method().on('success'{
* next();
*                                                                                                    . next( );
* })
* })
* .push( ... )
* .run() //Execute
* .on('success', function() {
*       allThings_is_down();
*    });
function typeOf( obj ){
return Object.prototype.toString.call( obj ).match(/[object ([^]]*)]/)[1];
}

function AsyncManager( arg ){
this.execArrys = [];
this.push( arg );
}
//Use the inheritance method provided by the system

require('util').inherits( AsyncManager, require('events').EventEmitter );

//Mark the number of successfully run functions

AsyncManager.prototype.succCount = 0;



//Add
AsyncManager.prototype.push = function( arg ) {

var This = this;
if( typeOf(arg) == 'Array' ){

arg.forEach( function(v,i){

This.execArrys.push( v );
          });

return this; //Chain one

};

//Execute

AsyncManager.prototype.run = function(){

var self = this;

If( this.succCount == this.execArrys.length ) {
//Events are triggered after all functions are successfully executed
this.emit( ​​'success' );
} else {
this.execArrys[ this.succCount ]( self.run.bind( self ) );
}

this.succCount ;
return this; //Chain one

};


exports = module.exports = function( arg ){
return new AsyncManager( arg );

}





Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn