JavaScript
31K subscribers
1.23K photos
10 videos
33 files
904 links
A resourceful newsletter featuring the latest and most important news, articles, books and updates in the world of #javascript πŸš€ Don't miss our Quizzes!

Let's chat: @nairihar
Download Telegram
❀1πŸ€”1🀩1
🀟 You Don’t Know Node.js EventLoop (30 min read)

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
What is the output?
Anonymous Quiz
21%
A D
33%
A C
22%
B C
24%
B D
❀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
❀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
❀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));
❀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
What is the output?
Anonymous Quiz
15%
19 19
53%
64 19
22%
64 64
10%
19 64
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
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
CHALLENGE

const handler = {
get(target, prop, receiver) {
if (!(prop in target)) {
return `no ${String(prop)}`;
}
return Reflect.get(target, prop, receiver);
},
set(target, prop, value) {
if (typeof value === 'number' && value < 0) {
throw new RangeError('negative');
}
return Reflect.set(target, prop, value);
}
};

const obj = new Proxy({ a: 1, b: 2 }, handler);
obj.c = 10;
console.log(obj.a, obj.c, obj.missing, obj.hasOwnProperty('a'));
❀2πŸ”₯2πŸ‘1