Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the callback function in Node.js

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of how to use the callback function in Node.js, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this Node.js callback function. Let's take a look.

Similarly, the callback mode is often used in Node.js, such as where the IAccord O operation occurs. By nesting callback functions within callback functions, you can control the order of things (using the fundamental starting point of callbacks), such as processing data with another function after reading data from the database.

The following is the simplest example:

Function a () {return 1} function b (aa) {return 2 + aa} / / call: var cym0c = b (a ()) / An is a function, but it is called as an argument in the B function console.log (c) / / the result shows 3

The above example is easy to understand, so let's introduce another concept: async

Look at the following code:

Var a = 0function bb (x) {console.log (x)} function timer (time) {setTimeout (function () {axi6}, time);} / / call: console.log (a) timer (3000) bb (a)

The above code is very simple, and the logic we need is that the initial value of the global variable an is 0, then after 3 seconds, let it be 6, and then print it out. It seems that the above code is fine and theoretically meets our logical requirements, but it turns out to be like this:

00

What's going on?

Because JS is an asynchronous execution language, although axi6 is allowed in the timer function, JS will not wait for the end of the time to jump out of the function, but will immediately execute the next statement (that is, calling the bb function). But at this time, 3 seconds is not over at all, and a has not yet been reassigned, so it is still printed out as 0.

You can solve this problem with a callback function:

Var a = 0function bb (x) {console.log (x)} function timer (time, callback) {setTimeout (function () {a = 6 callback (a);}, time);} / / call: console.log (a) timer (3000)

This time, a keyword callback has been added to the timer function, which means that this is not an ordinary parameter, but a function name. Cheer up, and the key point is:

Generally speaking, the parameter of a function refers to the entrance of passing variables from outside to inside to the body of the function, but it is quite the opposite with the addition of callback here. It refers to the exit of the body of the function to call the external function after completing a certain mission! At this point, you should understand what a "callback" is, which means to call an external function back.

In this example, when 3 seconds have elapsed, first await 6, then call the function bb (x) with the keyword callback (a), and the result shows:

06

This logic meets our needs.

In terms of writing, you can also write it in function form when calling timer without defining the function bb, or you can change the calling part to this, and the effect is exactly the same:

Console.log (a) timer (3000, function (x) {console.log (x)})

This method of writing the function name is no longer needed (the term is called "anonymous function"). It is more common and easier to understand in nodejs code, which translates into natural language: time for 3 seconds, and then go back to call the contents of function (x) when finished.

Asynchronous programming technology is widely used in nodejs programming in order to use hardware efficiently and not to cause synchronous blocking. In fact, nodejs is still an asynchronous operation realized by multithreading technology at the bottom, but ordinary users do not need to delve into its implementation method, we just need to do our asynchronous processing well.

This is the end of the article on "how to use callback functions in Node.js". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use the callback function in Node.js". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report