
- 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
NodeJS - eventTarget.dispatchEvent() method
The eventTarget.dispatchEvent() method is used to dispatch an Event at the specified EventTarget, invoking the affected EventListeners in the order in which they have been added.
This method belongs to EventTarget class of node:events module.
Syntax
Following is the syntax of the NodeJs eventTarget.dispatchEvent() method −
eventTarget.dispatchEvent(event)
Parameters
This method accepts only one parameter.
- event: This parameter holds the Event object to be dispatched.
Return value
This methods return value is false if at least one of the event handlers which are handling that specific event called Event.preventDefault() method else, it returns true.
Example 1
Following is the basic example of the NodeJs eventTarget.dispatchEvent() Method.
Initially, we imported the node:events module. Then we created an Event with a constructor. Then we added a listener to the event named event. Then we dispatched the Event by calling eventTarget.dispatchEvent() with event as a parameter to it.
const { EventEmitter, listenerCount } = require('node:events'); const { eventNames } = require('node:process'); const eventtarget = new EventTarget(); const event = new Event('build'); // Listen for the event. eventtarget.addEventListener('build', (event) => { console.log(Hello) }, false); // Dispatch the event. eventtarget.dispatchEvent(event);
Output
Hello
Example 2
In this program, it doesn't directly use dispatchEvent() but it works for a similar purpose.
const { EventEmitter } = require('events'); class MyEmitter extends EventEmitter {} const myEmitter = new MyEmitter(); myEmitter.on('event', () => { console.log('an event occurred!'); }); myEmitter.emit('event');
Output
an event occurred!