Skip to content
Related Articles

Related Articles

Node.js assert.doesNotReject() Function

Improve Article
Save Article
  • Last Updated : 14 May, 2022
Improve Article
Save Article

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!