JavaScript
31K subscribers
1.23K photos
10 videos
33 files
900 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
CHALLENGE

async function main() {
const settled = await Promise.allSettled([
Promise.resolve(1),
Promise.reject('err'),
]);
const statuses = settled.map(s => s.status).join(',');

let anyResult;
try {
await Promise.any([Promise.reject('a'), Promise.reject('b')]);
} catch (err) {
anyResult = `${err.name}:${err.errors.join(',')}`;
}

console.log(statuses, anyResult);
}

main();
CHALLENGE

const output = [];
output.push(typeof foo);
try {
output.push(typeof bar);
} catch (e) {
output.push(e.message);
}
var foo = () => 'foo';
let bar = () => 'bar';
console.log(output.join(' | '));
🤔1
CHALLENGE

const arr = [1, [2, 3], [4, [5, 6]]];
const a = arr.flat();
const b = arr.flat(Infinity);
const c = arr.flatMap(x => Array.isArray(x) ? x : [x, x]);
console.log(a, b, c);
❤3
CHALLENGE

function* inner() {
const x = yield 1;
yield x + 1;
return 100;
}

function* outer() {
const result = yield* inner();
yield result;
}

const g = outer();
console.log(g.next().value, g.next(10).value, g.next().value, g.next().value);
❤1
CHALLENGE

const obj = {
value: 42,
getValue: function() {
return this.value;
}
};

const unboundGetValue = obj.getValue;
const boundGetValue = unboundGetValue.bind(obj);
const boundTwice = boundGetValue.bind({ value: 100 });

console.log(
boundGetValue(),
boundTwice(),
unboundGetValue.call({ value: 7 }),
obj.getValue.apply({ value: 99 })
);
❤4
💻 Deno is Joining Cloudflare (But…)

Maybe David Bushell saw the writing on the wall, as the Deno team isn't joining Cloudflare to keep working on Deno, but to fold celld into workerd so Durable Objects can be self-hosted (more on that here). The Deno runtime will get a final year of official fixes, Deno Deploy closes in six months, and JSR moves to Cloudflare's infrastructure.

Ryan Dahl
Please open Telegram to view this post
VIEW IN TELEGRAM
❤1👍1🤔1
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));
❤1