- 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 - Streams
- Node.js - 文件系统
- Node.js MySQL
- Node.js - MySQL 快速入门
- Node.js - MySQL创建数据库
- Node.js - MySQL创建表
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where 子句
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB 快速入门
- Node.js - MongoDB 创建数据库
- Node.js - MongoDB 创建集合
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB 查询
- Node.js - MongoDB 排序
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js模块
- Node.js - 模块
- Node.js - 内置模块
- Node.js - utility 模块
- Node.js - Web 模块
Node.js - assert.doesNotReject() 函数
Node.js assert.doesNotReject() 函数是 Node.js 的 assert 模块的内置函数。它用于检查给定的承诺是否未被拒绝。
在 assert.doesNotReject() 函数中,如果提供的参数是 promise,它将等待 promise。如果提供的参数是一个函数,则立即调用它并等待返回承诺完成。此函数将检查提供的承诺是否被拒绝。此函数的行为与 assert.doesNotThrow() 相同。
assert.doesNotReject() 使用起来没有用,因为捕获错误并再次拒绝错误只有一点好处。相反,如果我们向不应被拒绝的特定代码路径添加适当的注释,并尽可能保持错误消息的表现力,那就更好了。
语法
以下是 assert.doesNotReject() 函数Node.js的语法 -
assert.doesNotReject(asyncFn[, error][, message]);
参数
此函数接受三个参数。下面将对此进行描述。
- asyncFn − (必需) 此参数包含一个异步函数,该函数将同步抛出错误。
- error − (可选)此参数可以保存类、正则表达式、验证函数或将测试每个属性的对象。
- message − (可选)此参数可以保存类、正则表达式、验证函数或将测试每个属性的对象。
返回值
函数 assert.doesNotReject() 会将被拒绝的承诺返回到输出中。
例以下是 assert.doesNotReject() 函数的用法Node.js。
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.
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.
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.