The encounter between Thunkify and co

Source: Internet
Author: User

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

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.