Node.js 同步函数相对于异步函数的优势是什么

Node.js 同步函数相对于异步函数的优势是什么

Node.js为我们提供了一个内置fs(文件系统)模块,用于处理不同的文件操作任务,例如读取文件、写入文件、删除文件等等。可以使用以下语法来安装fs模块:

语法:

npm introduce fs --save
JavaScript

注意: 上述npm代表的是Hub Bundle Supervisor,可以在其中安装Node.js的所有条件。

为了使用fs模块,将以下语句添加到代码中:

const fs = require('fs');
JavaScript

fs模块具有各种处理文档的任务,如读取记录,编写文档,附加文档,关闭文档,删除记录等。每个操作都可以根据用户的要求以同步或异步的方式执行。

同步方法: 同步函数会阻塞程序的执行,直到完成文件操作。这些函数也被称为阻塞函数。异步方法的最后一个参数是文件描述符。文件描述符是对打开的文件的引用。它是在使用fs模块的fs.open()方法打开文件后返回的文件号或引用ID。所有异步方法都可以通过在函数名后添加“Sync”来同时执行。Node.js中fs模块的一些异步方法包括:

  • fs.readFileSync()方法
  • fs.renameSync()方法
  • fs.writeSync()方法
  • fs.writeFileSync()方法
  • fs.fsyncSync()方法
  • fs.appendFileSync()方法
  • fs.statSync()方法
  • fs.readdirSync()方法
  • fs.existsSync()方法

同步学习的好处:

  • 参与者之间的互动。
  • 参与者之间的知识和经验交流。
  • 教练获得即时反馈。
  • 培训按固定日程进行。

示例1:同步读取技术:

让我们创建一个名为main.js的JavaScript文件和一个名为sample.txt的文本文件,其中包含以下语句:

GeeksForGeeks is a Computer Science portal.
JavaScript

将附带的代码添加到main.js文件中并执行:

const fs = require("fs");
 
// Synchronous read
console.log("Synchronous read method:");
const information = fs.readFileSync('sample.txt');
console.log("Data in the document is - "
            + data.toString());
JavaScript

输出:

Node.js 同步函数相对于异步函数的优势是什么

示例2:同步附加策略

让我们创建一个名为main.js的JavaScript记录和一个名为sample.txt的文本文档,其中包含以下断言:

Hi World !
JavaScript

将附带的代码添加到main.js文件中并执行:

在main.js文件中添加以下代码并执行:

const fs = require("fs");
 
// Coordinated read
console.log("Synchronous attach method:");
const information = "\nGeeksForGeeks is a Computer Science entrance.";
 
// Attach information to record
fs.appendFileSync('sample.txt', information, 'utf8');
console.log("Data is attached to record effectively.")
information = fs.readFileSync('sample.txt');
console.log("Data in the record subsequent to adding is - \n" +
            data.toString());
JavaScript

输出:

Node.js 同步函数相对于异步函数的优势是什么

异步方法: 异步函数不会阻碍程序的执行,每个命令在之前的命令之后执行,不管之前的命令是否已注册结果。之前的命令在后台运行,并在处理完成后负责结果。因此,这些函数被称为非阻塞函数。它们接受一个回调函数作为最后一个参数。相较于同步函数,异步函数更受欢迎,因为它们不会阻塞程序的执行,而同步函数会在处理完成前阻塞程序的执行。在Node.js的fs模块中,一些异步方法有:

  • fs.readFile() 方法
  • fs.rename() 方法
  • fs.write() 方法
  • fs.writeFile() 方法
  • fs.fsync() 方法
  • fs.appendFile() 方法
  • fs.stat()方法
  • fs.readdir() 方法
  • fs.exists() 方法

时间消耗较大的任务,例如从数据库查询大数据,应该使用异步方式处理,因为其他操作仍然可以执行,从而减少程序执行的时间。

异步学习的好处:

  • 参与者可以根据自己的时间和日程自行学习。
  • 对培训师和人力资源经理来说,工作量较小。
  • 自动化任务减少了如在线授课和批改考试等重复性工作。
  • 员工和客户在工作时间内在教室或大礼堂的时间较少
  • 没有与他人的实时互动。

示例1:异步读取技术

阶段1: 让我们创建一个名为main.js的JavaScript文件和一个名为sample.txt的文本文件,其中包含以下语句:

GeeksForGeeks is a Computer Science portal.
JavaScript

阶段2: 将附带的代码添加到main.js文件中并执行:

const fs = require("fs");
 
// Offbeat read
console.log("Asynchronous read method:");
fs.readFile('sample.txt', work(blunder, information){
    in the event that(blunder) {
        return console.error(err);
    }
console.log("Data in the document is - "
            + data.toString());
});
JavaScript

输出:

Node.js 同步函数相对于异步函数的优势是什么

示例2:异步附录技术

阶段1: 让我们创建一个名为main.js的JavaScript文档,以及一个名为sample.txt的文本记录,其中包含以下断言:

Hi World !
JavaScript

阶段2: 将附带的代码添加到main.js文件中并执行:

const fs = require("fs");
const information = 
    "\nGeeksForGeeks is a Computer Science entrance.";
 
// Asynchronously attaching information to document 
fs.appendFile('sample.txt', information, 'utf8',
 
    // Callback work 
    function (err) {  
   
          // In event that(fail) toss blunder
        // In the event that no mistake
        console.log("Data is added to document effectively.")
    });
    fs.readFile('sample.txt', work(fail, information) {
        if(fail) {
            return console.error(err);
        }  
    console.log(
        "Data in the record in the wake of affixing: \n" +
         data.toString()
    );
});   
JavaScript

输出:

Node.js 同步函数相对于异步函数的优势是什么

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册