Home Web Front-end JS Tutorial Blocking and non-blocking calls of node.js callback functions

Blocking and non-blocking calls of node.js callback functions

May 16, 2016 pm 03:32 PM
node.js Callback

First of all, node.js, as a JavaScript running platform, adopts event-driven and asynchronous programming. Through event registration and asynchronous functions, developers can improve resource utilization and server performance can also be improved. Secondly, for front-end people, node.js is the running platform for js. We can write system-level or server-side javascript code and hand it over to node.js for execution, so that our front-end people can also act on the background. In contrast , the browser-side JavaScript code will be subject to various security restrictions when running, and has limited operations on the client system, while node.js is a comprehensive background runtime that provides JavaScript with many functions that other languages ​​can achieve. .

Let’s get back to the topic. First, I will introduce blocking calls to you. Please read below for details.

1. Blocking call (read the file before performing subsequent operations)

var fs = require("fs");
var data = fs.readFileSync('/fs.txt');
console.log(data.toString());
console.log("程序执行结束!");
Copy after login

Output result:

"File content"

"Program execution ends!"

2. Non-blocking calls (reading files and other operations are performed synchronously)

var fs = require("fs"); 
fs.readFile('/fs.txt',function(err,data){
if(err) return console.error(err);
console.log(data.toString());
});
console.log("程序执行结束!");
Copy after login

Output result:

"Program execution ends!"

"File content"

The above content is the node introduced by the editor The entire content of blocking calls and non-blocking calls of .js callback functions, I hope you like it.

【Recommended related tutorials】

1. JavaScript video tutorial
2. JavaScript online manual
3. bootstrap tutorial

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

An article about memory control in Node

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

Detailed graphic explanation of the memory and GC of the Node V8 engine

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

Let's talk in depth about the File module in Node

What should I do if node cannot use npm command? What should I do if node cannot use npm command? Feb 08, 2023 am 10:09 AM

What should I do if node cannot use npm command?

How to write java callback function How to write java callback function Jan 09, 2024 pm 02:24 PM

How to write java callback function

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

Let's talk about the event loop in Node

Learn more about Buffers in Node Learn more about Buffers in Node Apr 25, 2023 pm 07:49 PM

Learn more about Buffers in Node

Basic syntax and application of callback functions in Java Basic syntax and application of callback functions in Java Jan 30, 2024 am 08:12 AM

Basic syntax and application of callback functions in Java

See all articles