Home > Web Front-end > JS Tutorial > body text

A brief discussion on callbacks in Nodejs

青灯夜游
Release: 2021-05-19 09:47:25
forward
2561 people have browsed it

This article will introduce to you the callback callback in Nodejs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on callbacks in Nodejs

Recommended study: "nodejs tutorial"

What is callback? Obviously, the literal meaning is Callback

So why do we need callback operations in Node? Students who know Node may know that it is to handle the results of asynchronous operations.

The following is a detailed explanation of this issue:

Before talking about the callbackcallback of asynchronous operations, let’s first take a look at the synchronous operation mode:

function processData () {
  var data = fetchData ();
  data += 1;
  return data;
}
Copy after login

This synchronization mode can work normally under normal circumstances, and this operation is also typical in other development environments. Then it can be clearly seen that if the fectData method is very time-consuming when loading data, it will cause the entire program to block before the data synchronization loading is completed.

Node.js is an asynchronous operation processing platform. Its use of callbackcallback will not be blocked by file I/O operations. A callback function is a function that is executed after a given task has completed; this approach avoids some blocking situations while allowing other code to continue executing.

We use the callback form of Node.js to handle the above synchronization operationfetchDataTime-consuming situation:

function processData (callback) {
  fetchData(function (err, data) {
    if (err) {
      console.log("An error has occurred. Abort everything!");
      return callback(err);
    }
    data += 1;
    callback(data);
  });
}
Copy after login

At first glance, it does seem a bit complicated , not easy to understand, but really important, because callbacks handling asynchronous operations are the basis of Node.js.

callbackThe meaning of callback is: after your asynchronous operation is completed, you will execute the content in my callbackcallback.

Node.js's form of callback processing asynchronous operations allows us to handle as many concurrent operations as possible (the operations are similar to IO operations)

For example:

In On a web service, there were several previous requests that were blocked for some reason, and at the same time, hundreds or even thousands of requests will be issued in the future;

How will Node.js handle this asynchronous blocking situation? Woolen cloth?

It will execute blocking queries asynchronously so that subsequent query requests can work normally, instead of waiting for the previous blocking request to end before processing subsequent requests.

The following is the typical format for using an asynchronous function:

function asyncOperation ( a, b, c, callback ) {
  // ... lots of hard work ...
  if ( /* an error occurs */ ) {
    return callback(new Error("An error has occurred"));
  }
  // ... more work ...
  callback(null, d, e, f);
}

asyncOperation ( params.., function ( err, returnValues.. ) {
  //This code gets run after the async operation gets run
});
Copy after login

callback is generally the last parameter of the asynchronous function. When the asynchronous operation ends, call thiscallback.

Generally, the first parameter of callback is error

If an asynchronous operation encounters an error, it will call callback, the first parameter is an Error object; otherwise, the first error is null, and other parameters are returned normally.

English original address: https://nodejs.org/en/knowledge/getting-started/control-flow/what-are-callbacks/

More For programming related knowledge, please visit: programming video! !

The above is the detailed content of A brief discussion on callbacks in Nodejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template