node. JS callback function

Source: Internet
Author: User

The immediate manifestation of node. JS asynchronous programming is callbacks.

Asynchronous programming relies on callbacks to implement, but can not say that the use of callbacks after the program is asynchronous, the callback function will be called after the completion of the task, node uses a large number of callback functions, node so the API is to support the callback function. The following are examples of blocking and non-blocking.

Block Code instance:

First create a file Input.txt, as follows:

This is a test file, O (∩_∩) o hahaha ~.

Create main.js with the following code:

var fs=require ("FS");

var data=fs.readfilesync (' Input.txt ');

Console.log (Data.tostring ());

Console.log ("The program execute to the end");

Non-blocking code:

Main.js's Code:

Fs=require (' Input.txt ');

Fs.readfile (' Input.txt ', function (error,data) {

if (error) return Console.error (Error);

Console.log (Data.tostring ());

});

Console.log ("The program execute to the end");

node. JS 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.