CHALLENGE
class EventBus {
#listeners = new Map();
on(event, cb) {
if (!this.#listeners.has(event)) this.#listeners.set(event, []);
this.#listeners.get(event).push(cb);
return this;
}
emit(event, ...args) {
const cbs = this.#listeners.get(event) || [];
return cbs.map(cb => cb(...args));
}
}
const bus = new EventBus();
bus.on('data', x => x * 2).on('data', x => x + 1);
const results = bus.emit('data', 5);
console.log(results, bus.emit('missing', 1));β€3
What is the output?
Anonymous Quiz
17%
[ 10, 11 ] []
45%
[ 10, 6 ] undefined
34%
[ 10, 6 ] []
3%
[ 5, 6 ] []
β€1π€1π€©1
You might be wondering if the title of this article is a bit ambitious, but fear not! Iβve put in the time and effort to cover everything you need to know about the EventLoop in Node.js. And believe me, youβll discover so many new things along the way!
nairihar
Please open Telegram to view this post
VIEW IN TELEGRAM
β€6π1π₯1
CHALLENGE
const x = 0;
const y = '';
const z = null;
const r1 = x ?? y ? 'A' : 'B';
const r2 = z ?? y ? 'C' : 'D';
console.log(r1, r2);
π€©1
β€2π₯1
CHALLENGE
async function run() {
const results = await Promise.allSettled([
Promise.resolve(1),
Promise.reject('error'),
new Promise(resolve => setTimeout(() => resolve(3), 0)),
]);
console.log(results.map(r => r.value ?? r.reason).join(','));
}
run();β€3π1
β€2
CHALLENGE
class Counter {
#count = 0;
get value() {
return this.#count;
}
set value(v) {
if (v < this.#count) throw new RangeError('Cannot decrease');
this.#count = v;
}
}
const c = new Counter();
c.value = 5;
let msg;
try {
c.value = 3;
} catch (e) {
msg = e.message;
}
c.value = 10;
console.log(msg, c.value);π₯2
What is the output?
Anonymous Quiz
20%
RangeError 10
59%
Cannot decrease 10
14%
undefined 10
7%
Cannot decrease 3
β€1π1π€1
CHALLENGE
const obj = { b: 1, 2: 'two', a: 3, 1: 'one' };
console.log(Object.keys(obj).join(','), Object.values(obj).join(','));π€3
What is the output?
Anonymous Quiz
17%
1,2,a,b one,two,3,1
50%
b,2,a,1 1,two,3,one
27%
1,2,b,a one,two,1,3
6%
a,b,1,2 3,1,one,two
β€6π1
CHALLENGE
class Counter {
static #total = 0;
#id;
constructor() {
Counter.#total++;
this.#id = Counter.#total;
}
get id() { return this.#id; }
static get total() { return Counter.#total; }
}
class Special extends Counter {
#label;
constructor(label) {
super();
this.#label = label;
}
get label() { return `${this.#label}-${this.id}`; }
}
const c1 = new Counter();
const c2 = new Special('x');
const c3 = new Special('y');
console.log(c1.id, c2.id, c3.label, Counter.total);β€1
π€£2β€1
CHALLENGE
const source = {
count: 0,
get value() {
this.count++;
return this.count;
}
};
const { value, ...rest } = source;
const spread = { ...source };
console.log(value, spread.value, source.count, JSON.stringify(rest));What is the output?
Anonymous Quiz
35%
1 2 2 {"count":1}
39%
1 2 1 {"count":0}
13%
2 2 2 {"count":2}
13%
1 1 1 {"count":1}
β€2π₯1π€£1
CHALLENGE
const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x);
const pipe = (...fns) => x => fns.reduce((acc, fn) => fn(acc), x);
const addOne = x => x + 1;
const double = x => x * 2;
const square = x => x * x;
console.log(compose(square, double, addOne)(3), pipe(square, double, addOne)(3));
π€©1
CHALLENGE
let i = 0;
const key = () => `k${i++}`;
const obj = { [key()]: 1, [key()]: 2, [key()]: 3 };
const sym = Symbol('x');
const keyObj = { toString() { return 'dyn'; } };
const obj2 = {
[sym]: 'secret',
[`a${1 + 1}`]: 'computed',
['a' + 2]: 'overwritten',
[keyObj]: 'fromObj'
};
console.log(`${Object.keys(obj).join(',')} ${obj2.a2} ${obj2.dyn} ${obj2[sym]}`);
π3β€1
What is the output?
Anonymous Quiz
22%
k0,k1,k2 computed fromObj secret
44%
k1,k2,k3 overwritten fromObj secret
23%
k0,k1,k2 overwritten dyn secret
11%
k0,k1,k2 overwritten fromObj secret
π3
CHALLENGE
const memoize = (fn) => {
const cache = new Map();
return (...args) => {
const key = JSON.stringify(args);
if (cache.has(key)) return cache.get(key);
const result = fn(...args);
cache.set(key, result);
return result;
};
};
let calls = 0;
const square = memoize((x) => {
calls++;
return x * x;
});
console.log(square(4), square(4), square(-4), calls);β€2
β€2