Node.js Callback Problem

Source: Internet
Author: User

Node.js need to perform asynchronous logic sequentially in a sequential fashion, that is, to encapsulate subsequent logic in a callback function as a parameter of the starting function, nested in layers. Although this style can improve CPU utilization, reduce the waiting time, but when the subsequent logical steps will affect the readability of the code, the resulting code modification maintenance becomes very difficult. According to the appearance of this code, it is generally called "Callback Hell" or "Pyramid of Doom", this article called the callback Pit, the more nested, the deeper the pit.

The origin of the pit

Subsequent delivery style

Why would there be a pit? This starts with the subsequent delivery style (Continuation-passing Style--cps). This programming style was first presented by Gerald Jay Sussman and Guy L Steele, Jr. On Ai Memo 349, the year was 1975, the schema language's debut. Since JavaScript's functional programming principles derive primarily from schemas, this style has naturally been brought into JavaScript.

This style of function should have additional parameters: "Subsequent logic bodies", such as functions with one parameter. The CPS function calculates the result value and does not return directly, but calls that subsequent logical function and takes the result as its argument. In order to realize the transfer between logical steps and the continuation of logic. In other words, if you want to invoke the CPS function, the caller function provides a subsequent logical function to receive the "return" value of the CPS function.

Callback

In JavaScript, this "follow-on logic" is what we often call a callback (callback). This function as a parameter is called a callback because it is generally defined in the main program, given to the library function by the main program, and called back when it is needed. The callback function, as a parameter, is typically an asynchronous function that takes a long time to be executed by another thread so that it does not affect subsequent operations of the main program. As shown in the following illustration:

In JavaScript code, the subsequent delivery style calls the incoming callback function at the logical end of the CPS function and passes the result to it. However, this style is not generally required when asynchronous functions that do not require lengthy processing are required. Let's look at a simple example of programming a simple 5-element equation:

X+y+z+u+v=16
x+y+z+u-v=10
x+y+z-u=11
x+y-z=8
x-y=2

For x+y=a;x-y=b, we all know how to solve this simple two-element equation, and the 5-element equation is no different from the binary equation, which is the two-way addition divided by 2 to get the previous part, two-way subtraction divided by 2 to find the latter part. The first part of the 5-yuan equation is the value of the 4-dimensional equation, and so on. Our program is written to:

Code Listing 1. General Solution-calnorm.js

var res = new Int16array ([16,10,11,8,2]), l= res.length;
var variables = [];
for (var i = 0;i < l;i++) {
    if (i = = l-1) {
        variables[i] = res[i];
    } else {
      Variables[i] = Calculatetail (res[i],res[i+1]);
      Res[i+1] = Calculatehead (res[i],res[i+1]);
    }
function Calculatetail (x,y) {return
    (x-y)/2;
}
function Calculatehead (x,y) {return
    (x+y)/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.