
- 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 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線
- 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 - assert.doesNotReject() 函式
Node.js assert.doesNotReject() 函式是 Node.js 的 assert 模組的內建函式。它用於檢查給定的 Promise 是否沒有被拒絕。
在 assert.doesNotReject() 函式中,如果提供的引數是 Promise,則等待 Promise 完成。如果提供的引數是函式,則立即呼叫該函式並等待返回的 Promise 完成。此函式將檢查提供的 Promise 是否被拒絕。此函式的行為與 assert.doesNotThrow() 相同。
assert.doesNotReject() 的實用性不大,因為捕獲錯誤並再次拒絕錯誤的好處很少。相反,如果我們為不應該被拒絕的特定程式碼路徑新增適當的註釋,並儘可能使錯誤訊息具有表達性,會更好。
語法
以下是 Node.js assert.doesNotReject() 函式 的語法:
assert.doesNotReject(asyncFn[, error][, message]);
引數
此函式接受三個引數。如下所述。
asyncFn − (必需) 此引數包含一個非同步函式,該函式將同步丟擲錯誤。
error − (可選) 此引數可以包含類、正則表示式、驗證函式或物件,其中每個屬性都將被測試。
message − (可選) 此引數可以包含類、正則表示式、驗證函式或物件,其中每個屬性都將被測試。
返回值
函式 assert.doesNotReject() 將把被拒絕的 Promise 返回到輸出。
示例
以下是 Node.js assert.doesNotReject() 函式的使用方法。
const assert = require('assert'); (async () => { await assert.doesNotReject( async () => { throw new TypeError('Wrong value'); }, TypeError ); } )();
輸出
以下是上述程式碼的輸出:
(node:40094) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: Got unwanted rejection. Actual message: "Wrong value" at process._tickCallback (internal/process/next_tick.js:68:7) at Function.Module.runMain (internal/modules/cjs/loader.js:746:11) at startup (internal/bootstrap/node.js:238:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3) (node:40094) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:40094) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
示例
以下是 Node.js assert.doesNotReject() 函式在另一種場景下的使用方法。
const assert = require('assert'); (async () => { await assert.doesNotReject( async () => { throw new TypeError('Error occured!!!'); }, SyntaxError ); } )();
輸出
以下是上述程式碼的輸出:
(node:43403) UnhandledPromiseRejectionWarning: TypeError: Error occured!!! at assert.doesNotReject (/home/cg/root/639c2bf348ea8/main.js:6:23) at waitForActual (assert.js:518:21) at Function.doesNotReject (assert.js:620:39)at /home/cg/root/639c2bf348ea8/main.js:4:22 at Object.<anonymous> (/home/cg/root/639c2bf348ea8/main.js:11:2) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3) (node:43403) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:43403) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
nodejs_assert_module.htm
廣告