CHALLENGE
class ValidationError extends Error {
constructor(message, field) {
super(message);
this.name = "ValidationError";
this.field = field;
}
}
class NetworkError extends ValidationError {
constructor(message, field, statusCode) {
super(message, field);
this.name = "NetworkError";
this.statusCode = statusCode;
}
}
const err = new NetworkError("Not Found", "endpoint", 404);
console.log([
err instanceof NetworkError,
err instanceof ValidationError,
err instanceof Error,
err instanceof TypeError,
].join(", "));
β€3π2π₯1
What is the output?
Anonymous Quiz
30%
true, true, false, false
24%
true, false, true, false
43%
true, true, true, false
3%
false, true, true, false
Build composable parsers for CLIs with type safety, type inference, and built-in shell completion support, plus config file integration and man page generation from the same definitions. v1.0 is the first stable release and Hong compares it to Commander.js and explains why you'd use Optique.
Hong Minhee
Please open Telegram to view this post
VIEW IN TELEGRAM
β€4π₯2π1
CHALLENGE
function* counter(start, end) {
for (let i = start; i <= end; i++) {
yield i;
}
}
function* pipeline() {
const first = yield* counter(1, 3);
console.log("Counter done:", first);
yield "bridge";
const second = yield* counter(7, 9);
console.log("Counter done:", second);
}
const gen = pipeline();
const results = [];
let next = gen.next();
while (!next.done) {
results.push(next.value);
next = gen.next();
}
console.log(results);β€7π2π₯1
What is the output?
Anonymous Quiz
26%
[1, 2, 3, 'bridge', 7, 8, 9] Counter done: undefined Counter done: undefined
31%
[1, 2, 3, 'bridge', 7, 8, 9] Counter done: 3 Counter done: 9
32%
Counter done: undefined Counter done: undefined [ 1, 2, 3, 'bridge', 7, 8, 9 ]
12%
Counter done: 3 Counter done: 9 [1, 2, 3, 'bridge', 7, 8, 9]
π₯3β€2π2π€©2
CHALLENGE
const config = {
host: "localhost",
port: 3000,
db: {
name: "mydb",
maxConnections: 10
}
};
Object.freeze(config);
config.port = 9999;
config.db.maxConnections = 99;
config.newProp = "surprise";
delete config.host;
const sealed = Object.seal({ x: 1, y: 2 });
sealed.x = 100;
sealed.z = 999;
delete sealed.y;
console.log(config.port, config.db.maxConnections, config.host);
console.log(sealed.x, sealed.y, sealed.z);
π2
What is the output?
Anonymous Quiz
25%
3000 10 localhost
19%
9999 10 undefined
25%
9999 99 undefined
31%
3000 99 localhost 100 2 undefined
β€6π€1
CHALLENGE
async function fetchData(id) {
if (id < 0) throw new Error("Invalid ID");
return { id, value: id * 10 };
}
async function process() {
const results = await Promise.allSettled([
fetchData(1),
fetchData(-1),
fetchData(3),
]);
results.forEach(({ status, value, reason }) => {
if (status === "fulfilled") {
console.log(`fulfilled: ${value.id} -> ${value.value}`);
} else {
console.log(`rejected: ${reason.message}`);
}
});
}
process();
β€5π3π€2
If you donβt read the specs or endless posts about new language features, this is a great way to catch up. Most of the features are supported in Node, like Promise.try, Set union/intersection/difference, Array.fromAsync, and using, with others soon to land, like Math.sumPrecise and Map.getOrInsert (in Node 26).
Neciu Dan
Please open Telegram to view this post
VIEW IN TELEGRAM
β€7π₯3π2π€1π€©1
CHALLENGE
const EventEmitter = (() => {
const listeners = new WeakMap();
return class {
constructor() {
listeners.set(this, {});
}
on(event, fn) {
const map = listeners.get(this);
(map[event] ??= []).push(fn);
return this;
}
emit(event, ...args) {
const map = listeners.get(this);
(map[event] ?? []).forEach(fn => fn(...args));
return this;
}
};
})();
const bus = new EventEmitter();
const log = [];
bus
.on("data", val => log.push(`A:${val}`))
.on("data", val => log.push(`B:${val}`))
.on("done", () => log.push("done"));
bus.emit("data", 1).emit("data", 2).emit("done");
console.log(log.join(" | "));
β€5π₯1
What is the output?
Anonymous Quiz
16%
A:1 | B:1 | done
38%
A:1 | A:2 | B:1 | B:2 | done
32%
A:1 | B:1 | A:2 | B:2 | done
14%
A:1 | B:2 | done
β€2π1
CHALLENGE
class Vehicle {
#speed = 0;
constructor(brand) {
this.brand = brand;
}
accelerate(amount) {
this.#speed += amount;
return this;
}
getSpeed() {
return this.#speed;
}
toString() {
return `${this.brand} @ ${this.#speed}km/h`;
}
}
class Car extends Vehicle {
#gear = 1;
constructor(brand, model) {
super(brand);
this.model = model;
}
shiftUp() {
this.#gear++;
return this;
}
toString() {
return `${super.toString()} [Gear ${this.#gear}]`;
}
}
const car = new Car("Toyota", "Supra");
car.accelerate(60).accelerate(40).shiftUp().shiftUp();
console.log(car.toString());
console.log(car instanceof Car);
console.log(car instanceof Vehicle);
console.log(Object.getPrototypeOf(Car) === Vehicle);
β€3π1
CHALLENGE
const delay = (ms, val) => new Promise(res => setTimeout(res, ms, val));
async function pipeline(...fns) {
return async (input) => {
let result = input;
for (const fn of fns) {
result = await fn(result);
}
return result;
};
}
const double = async (x) => delay(10, x * 2);
const addTen = async (x) => delay(10, x + 10);
const square = async (x) => delay(10, x ** 2);
(async () => {
const process = await pipeline(double, addTen, square);
const output = await process(3);
console.log(output);
})();
β€2π1π₯1
β€2π₯1
CHALLENGE
const inventory = new Map([
["sword", { quantity: 3, weight: 15 }],
["shield", { quantity: 1, weight: 25 }],
["potion", { quantity: 10, weight: 2 }],
]);
inventory.set("bow", { quantity: 2, weight: 8 });
inventory.delete("shield");
const totalWeight = [...inventory.entries()].reduce((acc, [item, { quantity, weight }]) => {
return acc + quantity * weight;
}, 0);
const heaviestItem = [...inventory.entries()].reduce((max, [item, data]) =>
data.weight > max.weight ? { name: item, ...data } : max,
{ name: "", weight: 0 }
);
console.log(inventory.size, totalWeight, heaviestItem.name);
β€3π2
β€4π2
CHALLENGE
const ops = {
add: (a, b) => a + b,
subtract: (a, b) => a - b,
multiply: (a, b) => a * b,
divide: (a, b) => b !== 0 ? a / b : NaN,
};
const pipeline = (...fns) => (value) => fns.reduce((acc, fn) => fn(acc), value);
const double = (x) => ops.multiply(x, 2);
const addTen = (x) => ops.add(x, 10);
const halve = (x) => ops.divide(x, 2);
const subtractThree = (x) => ops.subtract(x, 3);
const transform = pipeline(double, addTen, halve, subtractThree);
const results = [5, 0, -4].map(transform);
console.log(results);β€4π2π₯1
What is the output?
Anonymous Quiz
22%
[ 9, 2, -2 ]
36%
[ 7, 5, -3 ]
20%
[ 12, 5, 1 ]
23%
[ 7, 2, -2 ]
1β€5π₯2π1