Open In App

Node.js assert.doesNotReject() Function

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:

Return Value: It returns a rejected Promise.



Example 1:




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:




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


Article Tags :