Nonsense preface
Write for many years of JS, have encountered the most egg ache thing is callback hell, I believe we also feel empathy.
Many of the industry has put forward a lot of good solutions for this, I understand the main:
- Pauling event Proxy, easy to understand and simple to get started
- Lao Zhao's wind.js, writing the most comfortable, most able to express the program sequence of execution logic
- Promise, personal feeling to solve a hole to introduce another pit, write out the code a big lump, code readability is the worst
I this person idle nothing also love toss, I also build wheels, not for other only for their own code to write comfortable and smooth.
Portal: Currently only supports the node. JS Environment and later has time to increase browser support
- GitHub Https://github.com/flowforever/avril.queue
- NPM Install Avril.queue
Brief introduction
Why is called AVQ: the full name is avril.queue because elder brother likes Avril to be named with her name, has nothing to do with the island
Core: AVQ as an asynchronous process control framework with two main elements for asynchronous process control and easy coding
- Queue execution queues Ensure that program logic executes in sequence
- The $AwaitData asynchronously waits for the data object, which can be used as a parameter for the next asynchronous request, implementing the core of the coding fluency
Hands Avqbasic Example:q.func, Q. $await, Q. $each
var q = avq ();
var function / * This is not a point to Q, but a new AVQ () object so this is actually a subqueue, and This.error () will throw the error layer on the default and throw no processing on it. In later chapters I will detail the principle of q.func execution */this. Error (ERR); return res;}) ;
What to do if fs.readfile needs context
Q. $await, Q. $each, and later q.$ $await, q.$ $each support incoming context as the first parameter, for example
var function (Err, res) { this. Error (ERR); return res;}); var // the most $awaitData result of Res will be automatically var // by default, the first parameter existed number in the Fs.existe callback (existed) is returned as the value
varAVQ = require (' Avril.queue '));varQ =Avq ();
/*return $AwaitData Object*/var$fileContent = Q. $await (Fs.readfile, ' the/path/of/file.txt ', function(Err, filecontent) { This. Error (ERR); returnfilecontent;});/*convert the $awaitData ' result, return a new $AwaitData object*/var$ids = $fileContent. Convert (function($org) {return$org. Result (). split (' \ n ');}); /*return $AwaitData object which result is list of*/var$users = Q. $each (db. User.findbyid, $ids,function(err, user) { This. Error (ERR); returnuser;}); Q.func (function() {Console.log ($fileContent. Result ()) Console.log ($users. Realresult ());});
Syntax sugar:q.$ $await, q.$ $each, Q. $if ($awaitData, Truefunc), Q. $if ($awaitData, Truefunc). $else (), Q. $if ($awaitData, Truefunc). $elseIf ($otherAwaitData, Othertruefunc)
The general return value for standard node. JS asynchronous Calls is callback (err, result),
var // the difference with $await () is that the last parameter of $await requires Callback:return res;
$fileContent results are automatically used after the asynchronous request is complete.
varAVQ = require (' Avril.queue '));varQ =Avq ();var$fileContent, $ids, $users;/*return $AwaitData Object*/varfilePath1 = ' The/path/of/file1.txt ';varfilePath2 = ' The/path/of/file2.txt ';var$fileExisted =Q. $await (fs.exists, filePath1); q.$if($fileExisted,function() {$fileContent= This. $ $await (fs.readfile, filePath1);}). $Else(function() {$fileContent= This. $ $await (fs.readfile, filePath2);}); Q.func (function() {/*convert the $awaitData ' result, return a new $AwaitData object*/$ids= $fileContent. Convert (function($org) {return$org. Result (). split (' \ n '); }); /*return $AwaitData object which result is list of*/$users= This. $ $each (db. User.findbyid, $ids); }); Q.func (function() {Console.log ($fileContent. Result ())//only the first level result () is removedConsole.log ($users. Realresult ());//recursive removal of all result ()});
Summary
Nobody is perfect, yards no end code, welcome everyone to shoot bricks, if everyone is interested I will continue to launch more API documentation and introduction (everyone is not interested or to continue to write complete, haha).
I think this library is interesting, so let's get a github start.
JS Asynchronous Process Control AVQ (avril.queue)