- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- 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 Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
Node.js - assert.ifError() Function
The Node.js assert.ifError() function is used to test for errors in asynchronous operations and raise an error if one occurs.
It takes one argument, which should be either an Error object or a value that evaluates to false (such as undefined or null). If the argument is undefined or, null, it throws an error with the provided message. Otherwise it does nothing and returns undefined. This allows developers to write code that handles both synchronous and asynchronous errors without having to manually check for them each time.
Syntax
Following is the syntax of the Node.js assert.ifError() function −
assert.ifError(value);
Parameters
value − (required) The value we passed is stored in this parameter. This function throws this value if it is not undefined or null and the Input value can be any type.
Return Value
This function returns the AssertionError of the object type to output.
Example
In the following example below, we are passing 'undefined' to the value parameter of the Node.js assert.ifError() function.
const assert = require('assert'); var und = undefined; assert.ifError(und);
Output
On executing the above program, the ifError() function will not throw AssertionError because the value is undefined.
// Returns nothing
Example
In the example below, we are passing 'null' to the value parameter of the Node.js assert.ifError() function.
const assert = require('assert'); var NULL = null; assert.ifError(NULL);
Output
On executing the above program, the ifError() function will not throw AssertionError because the value is null.
// Returns nothing
Note − The function will throw AssertionError along with the value unless the value is undefined or null.
Example
In the example below, we are passing an integer to the value parameter of the function.
const assert = require('assert'); var num = 55; assert.ifError(num);
Output
On executing the above program, the function will throw AssertionError along with the value we passed.
assert.js:667 throw newErr; ^ AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 55 at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/main.js:4:8) 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) at Function.Module.runMain (internal/modules/cjs/loader.js:744:10) at startup (internal/bootstrap/node.js:238:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
Example
In the following example below, we are passing a string to the value parameter of the function.
const assert = require('assert'); var text = 'Tutorialspoint'; assert.ifError(text);
Output
On executing the above program, the function will throw AssertionError along with the value we passed.
assert.js:667 throw newErr; ^ AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'Tutorialspoint' at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/main.js:4:8) 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) at Function.Module.runMain (internal/modules/cjs/loader.js:744:10) at startup (internal/bootstrap/node.js:238:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
Example
In the following example below, we are passing an instance of an Error to the message parameter of the function.
const assert = require('assert'); var err = new Error('Error warning!!!!'); assert.ifError(err);
Output
So when we compile and run the code, the function will throw AssertionError and the value of Error will be thrown.
assert.js:667 throw newErr; ^ AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error warning!!!! at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/main.js:4:8) at Object.<anonymous> (/home/cg/root/639c30a0a5fb2/main.js:3:11) 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) at Function.Module.runMain (internal/modules/cjs/loader.js:744:10) at startup (internal/bootstrap/node.js:238:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
To Continue Learning Please Login
Login with Google