• Node.js Video Tutorials

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
廣告