On the role of thunkify, this article says about the collection of Thunkify and Co
There is a piece of code in the CO
/** Convert a thunk to a promise. * * @param {Function} * @return {promise} * @api private*/functionthunktopromise (FN) {varCTX = This; return NewPromise (function(Resolve, Reject) {Fn.call (CTX,function(Err, res) {if(ERR)returnreject (ERR); if(Arguments.length > 2) res = Slice.call (arguments, 1); Resolve (RES); }); });}
When you use thunkify to handle a function with callback, the co expects that the function you are dealing with will only accept a callback function on the previous one.
For example, var thunkfunc = thunkify (function (a,b,c,callback) {}) (A,b,c)
In the case of KOA, you can execute the function directly using yield thunkfunc.
Co detects that your iterator is returning a function that calls its own thunktopromise (Thunkfunc) to execute, in which the method is packaged into a PROMSE and executed and placed in a callback when executed. The callback has two parameters, err and res, and when done, call Promise's Resolve method to return the RES in callback (the CO value wants your callback to have two parameters). Co+thunkify to complete the function call, eliminating the process of adding callback.
The encounter between Thunkify and co