Rookie Learning nodejs--callback function

Source: Internet
Author: User

What is a callback function

If you have used jquery, then the function will be like the usual, for example, we often give an event a function parameter, in fact, this is the callback function, the callback function is what we call asynchronous, if it is a bit of a mask, then let us give an example.
We create a new index1.html and a index2.html file.
In index1.html

$(function(){    $(‘p‘).hide(‘slow‘,functin(){ alter("他隐藏了"); });});

In index2.html

function(){    $(‘p‘).hide(‘slow‘); alert(‘他隐藏了‘);}

Two functions, the first occurrence of the page's font is pop-up box and hide the P tag file is almost a colleague, even the first frame, the information slowly disappear, and the second function, p in the information slowly disappear, and then appear the box.

Callback functions in node. js

Create a new Callback.js file
Add the following code:

function haveBreakfast(food, drink, callback) {  console.log(‘早餐‘ + food + ‘, ‘ + drink); if (callback && typeof(callback) === "function") { callback(); }}haveBreakfast(‘toast‘, ‘coffee‘, function() { console.log(‘早餐吃完了,工作了啦!‘);});

The output results are as follows:

When to use callbacks

When to use it, let's look at an example and then explain it.
Create a new file1.txt, write the text aaaa, create a new file2.txt, write the text BBB, and then create a new file for Callback2.js

Add the following code to the Callback2:

var fs =Require' FS '), HTTP =Require' http '); Http.get ({host:' Baidu.com '},function(RES) {Console.log ("From Baidu.com"); }). On (' Error ',function(e) {Console.log ("Error occurred on the access page"); }); Fs.readfile (' File1.txt ',' UTF8 ',function(Err, data) {if (ERR)throw err; Console.log ( ' File is read ' + data);}); Http.get ({host:  ' www.sina.com.cn '},  function (res) {Console.log ( ' error ', function (E) {console.log ( ' File2.txt ',  ' UTF8 ', function  (err, data) {if (err) throw err; Console.log ( ' File2 is read, content is ' + data '); }); 

Two times the effect is as follows:

From the top of the results we can see, in fact, who first access, who access is not a fixed order, for the network access, we need to be based on the width of a lot of circumstances, for the file read, if the file is too large, they must be read out, and this situation formally we have to explain when to use, There is no strict requirement for order, but only the result, then OK, callback is your best choice.

Synchronous and asynchronous

node. JS wants us to use asynchronous programming in a single process, which is not unique to node. js or JavaScript, but a style, such as thread,runable in C #, Tread,task,java, and so on, in asynchronous programming, Important thing to say three times, it's a style, it's a style, it's a kind of style. If we use synchronization, the operation of the program may be blocked, which is not conducive to the use of our CPU.

Summary

We introduce the callback function in JS, we also describe how to use the callback function in node. js, and also introduce the difference between synchronous and asynchronous, of course we are recommended to use asynchronous. This will greatly increase the utilization of resources.

Rookie Learning nodejs--callback function

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.