Self-implemented async implements only one step function

Source: Internet
Author: User

Have to say, people and people's technology does have a gap, the same function, other people just have to write more elegant performance better

Whether it's C or JS

Self-function But look more ugly than others.

//The final effect is the same as async//The most commonly used serial and waterfall of individuals are now realized.//Implement Waterfall//Async.waterfall ([//function (callback) {//callback (NULL, ' One ', ' both ');//         },//function (arg1, arg2, callback) {//callback (NULL, ' three ');//         },//function (Arg1, callback) {////Arg1 now equals ' three '//callback (NULL, ' done ');//         }//], function (err, result) {////result now equals ' done '//     });varAsync = (function () {    //this is easier .    functionwhilst (Fntest, Fniterator, fnend) {if(Fntest ()) {Fniterator (function(err) {if(Err) {Fnend (Err)} whilst (Fntest, Fniterator, Fnend)}) } Else{fnend ()}}//The latter is the last column of the previous item only with the Modify next point    functionSerial (tasks, endcallback) {functionMakecallback (index) {varHasnext = (Index <tasks.length); if(hasnext) {varfn =function () {                    varargs =Array.prototype.slice (arguments)varNext = makecallback (index + 1) Args.push (next) tasks[index].apply (NULL, args)} Fn.hasnext=Hasnextreturnfn}Else return function() {Endcallback ()}} makecallback (0)()    }    functionWaterfall (Tasks, endcallback) {functionMakecallback (index) {varHasnext = (Index <tasks.length); if(hasnext) {varfn =function () {                    //error Handling is now required, if the first parameter is err then Endcallback                    //arguments is also going to start filtering from the first item .                    varargs = Array.prototype.slice (arguments, 1)                    if(arguments[0]) {endcallback (arguments[0],NULL) Endcallback=NULL                    } Else {                        varNextif(Index + 1 = =tasks.length) {Next=Endcallback}Else{Next= Makecallback (index + 1)                            //Args.push (next);                         }                        //This is just the latter function, followed by the previous one.                         //the last item cannot be obtained while the other parameters of the previous function.                         //Add the parameters of the next call to the front of Arg                         for(varKeyincharguments) {                            if(Key! = "0") {Args.push (Arguments[key]);                        }} args.push (next); Tasks[index].apply (NULL, args)} }                returnfn}//This is only called the end method, cannot pass the parameter, to pass the parameter, need to be same as before, put the latter item on the callback of the last item of tasks            //else return function () {            ////Return result            //endcallback (NULL)            // }        }        //This is the function returned directly by the call, and the next function calls the next.         //How do I upload the previous entry to the next item? Makecallback (0)()    }    return{waterfall:waterfall, serial:serial, Whilst:whilst}}) () async.serial ([function(callback) {Console.log ("1"); Callback ()},function(callback) {Console.log ("2"); Callback ()},function(callback) {Console.log ("3"); Callback ()}],function() {Console.log ("End")}) Async.waterfall ([function(callback) {Console.log ("1"); Callback (NULL, "a"); },    function(Arg1, callback) {Console.log ("arg1" + arg1); Console.log ("2"); Callback (NULL, "Err3", "D"); },    function(Arg1, Arg2, callback) {Console.log ("3"); Callback (NULL, "res"); }], function(err, result) {Console.log ("End"); Console.log (result);})varCount = 0; Async.whilst (function() {returnCount < 10; },    function(callback) {Count++; Console.log (count) setTimeout (callback,100); },    function(err) {//5 seconds have passed    }    );//iterators//(function () {//function Getiter (tasks) {//var i;//For (i = 0; i < tasks.length; i++) {//(function (index) {//var hasnext = ((index + 1) < tasks.length);//tasks[index].hasnext = Hasnext;//Console.log (Index + "" + Tasks[index].hasnext)//if (tasks[index].hasnext) {//Tasks[index].next = tasks[index + 1]//                 }//}) (i)//         }//return Tasks[0]//     }//var tasks = [//function () {Console.log ("1")},//function () {Console.log ("2")},//function () {Console.log ("3")},//function () {Console.log ("4")},//     ]//var task = Getiter (tasks)//While (task.hasnext) {//task ();//task = Task.next;//     }// })()

Self-implemented async implements only one step function

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.