
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境設定
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL WHERE 條件
- Node.js - MySQL ORDER BY 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL JOIN 連線
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制結果
- Node.js - MongoDB 連線
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - 回撥函式概念
什麼是回撥函式?
在 Node.js 中,回撥函式是函式的非同步等效項。它是一種特殊型別的函式,作為引數傳遞給另一個函式。Node.js 廣泛使用回撥函式。回撥函式幫助我們進行非同步呼叫。Node 的所有 API 都是以支援回撥函式的方式編寫的。
預設情況下,程式設計指令是同步執行的。如果程式中的某個指令需要執行冗長的過程,則主執行執行緒會被阻塞。只有在當前 I/O 完成後才能執行後續指令。這就是回撥函式發揮作用的地方。
當包含回撥函式作為引數的函式完成執行時,就會呼叫回撥函式,並允許程式碼在此期間執行。這使得 Node.js 具有高度的可擴充套件性,因為它可以處理大量請求,而無需等待任何函式返回結果。
在 Node.js 中實現回撥函式的語法如下:
function function_name(argument, function (callback_argument){ // callback body })
Node.js 中的 `setTimeout()` 函式是回撥函式的典型示例。以下程式碼呼叫非同步 `setTimeout()` 方法,等待 1000 毫秒,但不會阻塞執行緒。相反,後續的“Hello World”訊息會在計時訊息之前顯示。
示例
setTimeout(function () { console.log('This prints after 1000 ms'); }, 1000); console.log("Hello World");
輸出
Hello World This prints after 1000 ms
阻塞程式碼示例
要理解回撥函式的功能,請將以下文字儲存為 input.txt 檔案。
TutorialsPoint is the largest free online tutorials Library Master any technology. From programming languages and web development to data science and cybersecurity
以下程式碼使用 fs 模組中的 `readFileSync()` 函式同步讀取檔案。由於操作是同步的,它會阻塞其餘程式碼的執行。
var fs = require("fs"); var data = fs.readFileSync('input.txt'); console.log(data.toString()); let i = 1; while (i <=5) { console.log("The number is " + i); i++; }
輸出顯示 Node.js 讀取檔案並顯示其內容。只有在此之後,才會執行列印數字 1 到 5 的迴圈。
TutorialsPoint is the largest free online tutorials Library Master any technology. From programming languages and web development to data science and cybersecurity The number is 1 The number is 2 The number is 3 The number is 4 The number is 5
非阻塞程式碼示例
我們在以下程式碼中使用相同的 input.txt 檔案來演示回撥函式的使用。
TutorialsPoint is the largest free online tutorials Library Master any technology. From programming languages and web development to data science and cybersecurity
fs 模組中的 `ReadFile()` 函式提供了一個回撥函式。傳遞給回撥函式的兩個引數是錯誤和 `ReadFile()` 函式本身的返回值。當 `ReadFile()` 完成執行時,透過返回錯誤或檔案內容來呼叫回撥函式。在檔案讀取操作進行時,Node.js 會非同步執行後續迴圈。
var fs = require("fs"); fs.readFile('input.txt', function (err, data) { if (err) return console.error(err); console.log(data.toString()); }); let i = 1; while (i <=5) { console.log("The number is " + i); i++; }
輸出
The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 TutorialsPoint is the largest free online tutorials Library Master any technology. From programming languages and web development to data science and cybersecurity
箭頭函式作為回撥函式
您還可以將箭頭函式賦值為回撥函式引數。JavaScript 中的箭頭函式是匿名函式。它也稱為 lambda 函式。在 Node.js 中使用箭頭函式作為回撥函式的語法如下:
function function_name(argument, (callback_argument) => { // callback body })
它是在 JavaScript ES6 版本中引入的。讓我們用箭頭函式替換上述示例中的回撥函式。
var fs = require("fs"); fs.readFile('input.txt', (err, data) => { if (err) return console.error(err); console.log(data.toString()); }); let i = 1; while (i <=5) { console.log("The number is " + i); i++; }
上述程式碼產生的輸出與之前的示例類似。