


What is the difference between node synchronization and asynchronousness?
The difference between node synchronization and asynchronous is: synchronization means that the program runs from top to bottom. The next step can only be executed after the previous step is executed; asynchronous means that you do not need to wait for the above to run before running the following. Operation, asynchronous programming can be implemented by relying on callbacks, but it does not mean that the program after the callback is asynchronous.
The operating environment of this tutorial: windows10 system, nodejs version 12.19.0, Dell G3 computer.
What is the difference between node synchronization and asynchronousness
Synchronization means that the program runs from top to bottom, while asynchronous means that you do not need to wait for the above to run before running the following operations. . Asynchronous programming relies on callbacks, but it cannot be said that the program becomes asynchronous after using callbacks.
Synchronous English: sync (synchronization)
Asynchronous English: async (asynchronous)
Synchronous API: Only after the current API execution is completed, execution can continue Next API
console.log('before'); console.log('after');
Asynchronous API: The execution of the current API will not block the execution of subsequent code
console.log('before'); setTimeout( () => { console.log('last'); }, 2000); console.log('after');
The difference between synchronous API and asynchronous API (get return Value)
Synchronous API can get the result of API execution from the return value, but asynchronous API cannot (it seems that writing return in asynchronous API cannot get the result?)
// 异步 function getMsg () { setTimeout(function () { return { msg: 'Hello Node.js' } }, 2000); } const msg = getMsg (); //函数没有写return 默认是返回 undefined cnsole.log(msg); // 输出的是 undefined,因为定时器还没执行完就执行了输出
Callback function
Define your own function and let others call it.
// getData函数定义 function getData (callback) {} // getData函数调用 getData (() => {}); /*例子------------------*/ function getMsg (callback) { setTimeout(function () { callback ({ msg: 'Hello Node.js' }) }, 2000); } getMsg (function (msg) { console.log(msg); });
The difference between synchronous API and asynchronous API (code execution order)
The synchronous API is executed sequentially from top to bottom. The previous code will block the execution of the following code
The asynchronous API will not wait for the API execution to be completed before executing the code.
Code execution sequence analysis
JavaScript is divided into a synchronous code execution area and an asynchronous code execution area. Between them There is a callback function queue connection. First, JavaScript will execute all the contents of the synchronous code area, and then go to the asynchronous code area to execute the code, find the code block that has been executed in the asynchronous code area, and immediately find the callback function corresponding to the asynchronous code block and place it in the synchronous code execution area. To execute the asynchronous API
#read file API in Node.js, there is a callback function.
Event monitoring API also has callback functions. (The event processing function is the callback function, and the event listening API is the asynchronous API)
If the execution of the code behind the asynchronous API depends on the execution result of the current asynchronous API, but in fact the asynchronous API has not returned when the subsequent code is executed As a result, how to solve this problem?
For example, there is a requirement: read file A, file B, and file C in sequence (after reading A, then read B, then read C, it cannot be read at the same time)
If the above requirement is realized, It will cause the operation of reading B to be placed in the callback function of A, and the operation of reading C is placed in the callback function of B, resulting in many nesting levels (callback hell)
const fs = require('fs'); fs.readFile('./1.txt', 'utf8', (err, result1) => { console.log(result1) fs.readFile('./2.txt', 'utf8', (err, result2) => { console.log(result2) fs.readFile('./3.txt', 'utf8', (err, result3) => { console.log(result3) }) }) });
Recommended study: " nodejs video tutorial》
The above is the detailed content of What is the difference between node synchronization and asynchronousness?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

Yes, Node.js can be used for front-end development, and key advantages include high performance, rich ecosystem, and cross-platform compatibility. Considerations to consider are learning curve, tool support, and small community size.
