Open In App

Node.js assert.doesNotReject() Function

Last Updated : 14 May, 2022
Suggest changes
Share
Like Article
Like
Report

The assert.doesNotReject() method is used to check if the given promise is not rejected. If the provided parameter is a Promise, it is awaited; if it is a function, it is called immediately and the returning Promise is awaited.

Syntax:

assert.doesNotReject(asyncFn[, error][, message])

Parameters:

  • asyncFn: An asynchronous function or a Promise which is to be checked.
  • error: It is the specified error. It might be a regular expression or a function. This is optional.
  • message: The error message of string or error type. This is optional.

Return Value: It returns a rejected Promise.

Example 1:

JavaScript
import assert from 'node:assert/strict'; await assert.doesNotReject(  async () => {  await new Promise(resolve => setTimeout(resolve, 5000));  console.log("Hello");  },  SyntaxError ); 

Output:

Hello

Example 2:

JavaScript
import assert from 'node:assert/strict'; function resolved(result) {  console.log('Resolved'); }   function rejected(result) {  console.error(result); } await assert.doesNotReject(  Promise.reject(new Error('fail')).then(resolved, rejected),  SyntaxError ); 

Output:

 

Reference: https://nodejs.org/api/assert.html#assertdoesnotrejectasyncfn-error-message


Explore