Node JS CO analysis 2

Source: Internet
Author: User

Reprint Please specify: theviper http://www.cnblogs.com/TheViper

Better to see http://purplebamboo.github.io/2014/05/24/koa-source-analytics-2/.

Source

functionCo (FN) {varIsgenfun =isgeneratorfunction (FN); return function(done) {varCTX = This; //In tothunk () below we invoke Co ()    //with a generator, so optimize for    // this case    varGen =fn; //we only need to parse the arguments    //If Gen is a generator function.    if(isgenfun) {varargs = Slice.call (arguments), Len =args.length; varHascallback = Len && ' function ' = =typeofArgs[len-1]; Done= Hascallback?args.pop (): error; Gen= Fn.apply ( This, args); } Else{ Done= Done | |error;    } next (); //#92    //wrap the callback in a setimmediate    //So, any of it errors aren ' t caught by ' co '    functionexit (Err, res) {setimmediate (function() {Done.call (CTX, err, res);    }); }    functionNext (Err, res) {varret; //OK      if(!err) {        Try{ret=Gen.next (RES); } Catch(e) {returnexit (e); }      }      // Done      if(Ret.done)returnExitNULL, Ret.value); //NormalizeRet.value =Tothunk (Ret.value, CTX); //Run      if(' function ' = =typeofret.value) {varcalled =false; Try{ret.value.call (CTX,function(){            if(called)return; Called=true;          Next.apply (CTX, arguments);        }); } Catch(e) {setimmediate (function(){            if(called)return; Called=true;          Next (e);        }); }        return; }    }  }}

var isgenfun = isgeneratorfunction (FN), judging is not generator function, in fact, this from the example seems to be basically. Then determine if there is a callback. That is not there

Co (function *() {  var results = yield *foo ();  Console.log (results);   return results;}) (function(err,res) {    console.log (' res ')});

The function inside (err,res) {...}, if any, assigns it to done. Then Gen = fn.apply (this, args), which is the last article that says generator var it = start (); Start is a generator function, and functions are not yet executed.

Then next (), ret = Gen.next (res), start running the first yield size () inside *foo (), returning the object with the form {Value:[function],done:false}.

function *foo () {  var a = yield size (' node_modules/thunkify/.npmignore ');   var b = yield size (' node_modules/thunkify/makefile ');   var c = yield size (' Node_modules/thunkify/package.json ');   return [A, B, c];}

Then Ret.done judged whether it was done. If completed, exit () executes the callback. You can see that there are two parameters passed to the callback, err is the error message, and res is the result of yield execution return.

    function exit (Err, res) {      setimmediate (function() {        done.call (CTX, err, res);      });    

If not done, Ret.value = Tothunk (Ret.value, CTX); Formats the results returned for yield execution,

functiontothunk (obj, ctx) {if(isgeneratorfunction (obj)) {returnCo (Obj.call (CTX)); }  if(Isgenerator (obj)) {returnCo (obj); }  if(ispromise (obj)) {returnpromisetothunk (obj); }  if(' function ' = =typeofobj) {    returnobj; }  if(IsObject (obj) | |Array.isarray (obj)) {    returnObjecttothunk.call (CTX, obj); }  returnobj;}

If Ret.value is generator, continue Co (FN), if it is promise, return thunk form

function Promisetothunk (Promise) {  returnfunction(FN) {    promise.then (  function(res) {      fn (null, res);    }, FN);}  }

If it is a function, return the function.

If it's an object or an array, it's a little more complicated, this concludes.

Then if Ret.value is a function, execute the function and pass in a callback function,

function () {     ifreturn;      true ;     Next.apply (CTX, arguments);}

This is why the function to give yield is to write the form

function size (file) {  returnfunction(FN) {    function(err, stat) {       if return fn (err);      FN (null, stat.size);}    );}  }

When this asynchronous operation is executed, two parameters are passed to FN because function next (err, res).

In the previous article there is an example of changing the callback into a parameter, that is, FN (stat.size) instead of FN (null,stat.size), only return the first yield result, because there is no parameter shift, only through the position of the parameter to determine the parameters of the corresponding parameter, So stat.size is considered to be err, that is, the direct exit (), so the definition of thunk must be passed two parameters, and position can not be changed.

By the way, the thunkify mentioned in the previous article, it is used to change the general asynchronous operation into Thunk form, the source code is very simple

functionthunkify (FN) {assert (' function ' = =typeofFN, ' function required '); return function(){    varargs =NewArray (arguments.length); varCTX = This;  for(vari = 0; i < args.length; ++i) {args[i]=Arguments[i]; }    return function(done) {varcalled; Args.push (function(){        if(called)return; Called=true; Done.apply (NULL, arguments);      }); Try{fn.apply (ctx, args); } Catch(Err) {done (err); }    }  }};

Use

varSize=thunkify (fs.stat);function*foo () {varA = yield size (' node_modules/thunkify/.npmignore '); varb = yield size (' node_modules/thunkify/makefile '); varc = yield size (' Node_modules/thunkify/package.json '); return[A, B, c];} Co (function*(){  varResults = yield *foo ();  Console.log (results); returnresults;}) ();

The idea is to add parameters to the args array, and then to the args array

function () {        ifreturn;         true ;        Done.apply (null, arguments); }

This callback, as Fn.apply (CTX, args), executes the callback. It's done inside the co source.

function () {   ifreturn;    true ;   Next.apply (CTX, arguments);}

Done here is equivalent to writing the FN inside the thunk.

Thunk's summary is to encapsulate a return function (FN) outside of the asynchronous operation, and FN is the callback that the Co has passed to Thunk, which has next (). The FN is then triggered in the asynchronous operation callback to ensure that FN's next () is executed.

Go back to the main line, then Next.apply (ctx,arguments), and execute the next yield.

Finally, say the Objecttothunk in Tothunk ().

functionObjecttothunk (obj) {varCTX = This; varIsArray =Array.isarray (obj); return function(done) {varKeys =Object.keys (obj); varPending =keys.length; varResults =IsArray?NewArray (Pending)//predefine the array length:NewObj.constructor (); varfinished;//Prepopulate object keys to preserve key ordering    if(!IsArray) {       for(vari = 0; I < pending; i++) {Results[keys[i]]=undefined; }    }     for(vari = 0; i < keys.length; i++) {run (Obj[keys[i]], keys[i]); }    functionrun (FN, key) {if(finished)return; Try{fn=tothunk (FN, CTX); if(' function '! =typeoffn) {Results[key]=fn; return--pending | | DoneNULL, results); } fn.call (CTX,function(Err, res) {if(finished)return; if(Err) {Finished=true; returnDone (ERR); } Results[key]=Res; --pending | | DoneNULL, results);      }); } Catch(Err) {Finished=true;      Done (ERR); }    }  }}

Example

function *foo () {  var a= Yield {    name: {      first:yield size (' Node_ Modules/thunkify/.npmignore '),      last:yield size (' node_modules/thunkify/makefile ')    }  };   return A; // {name:{first:13,last:39}}

Object.keys (obj) adds all the keys of oject to an array. Here is the [' Name '],[' first ', ' last '], and then initializes the final returned results. then run ()

     for (var i = 0; i < keys.length; i++) {      run (Obj[keys[i]], keys[i]);    }

There is a layer of nesting in the example, so Obj[keys[i]] is still an object, which doesn't matter, it will be handled later.

Note that fn = Tothunk (FN, CTX), and if FN is an object, it calls Objecttothunk (obj). .... If there is a nesting in FN, Tothunk (FN,CTX) is deeply traversed.

If the returned FN is not a function, it is assumed to be nested at the end, which is the final function. --pending determines if key is the last of the key array, if it is done (NULL, results)

If FN is a function, it is almost the same as next (), the difference is that each function is executed once on the common length variable minus one, do not need to care about the order of execution of each function, as long as one of the functions found that the variable becomes 0 o'clock, representing the other functions are executed, is the last, You can then call the callback function done.

Node JS CO analysis 2

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.