EventEmitter once()

EventEmitter once()

Medium NodeJS Async & Event Loop 30 views
Explanation Complexity

Problem Statement

Listen to an event only once and print count.

Input Format

No input.

Output Format

Print integer.

Constraints

Emit twice but handler runs once.

Input / Output Format

Input Format
No input.
Output Format
Print integer.
Constraints
Emit twice but handler runs once.

Examples

Input:
Output:
1

Example Solution (Public)

NodeJS
const { EventEmitter } = require('events');
const ee = new EventEmitter();
let n = 0;

ee.once('meetcode', () => { n += 1; });
ee.emit('meetcode');
ee.emit('meetcode');
console.log(n);

Official Solution Code

const { EventEmitter } = require('events');
const ee = new EventEmitter();
let n = 0;

ee.once('meetcode', () => { n += 1; });
ee.emit('meetcode');
ee.emit('meetcode');
console.log(n);
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.