TGViewer
Channel Public Channel
JavaScript

JavaScript

@javascript

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
Subscribers
31.1K
Photos
1.2K
Videos
10
Links
894

Showing posts older than #3327 · Back to latest

Older Posts 20 shown
Post #3326 2.57K
  • 🤔 2
  • 👍 1
Post #3325 2.36K
CHALLENGE

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function processQueue(items) {
const results = [];

await items.reduce(async (prevPromise, item) => {
await prevPromise;
await delay(0);
results.push(item * 2);
}, Promise.resolve());

return results;
}

processQueue([1, 2, 3, 4])
.then((data) => console.log("Result:", data))
.catch((err) => console.log("Error:", err.message));

Promise.resolve()
.then(() => console.log("Microtask A"))
.then(() => console.log("Microtask B"));
  • ❤ 5
Post #3324 2.58K
💻 Desktop Apps With deno desktop

Deno 2.9 (or the 'canary' build now) can turn JavaScript projects into self-contained apps on macOS, Windows, and Linux. Unlike Electron, you can opt to use the default OS WebView or a bundled Chromium backend, plus you get cross-compilation and automatic support for apps built on frameworks like Next.js and SvelteKit.

The Deno Project
  • ❤ 8
  • 👍 6
  • 🔥 2
Post #3322 2.58K
CHALLENGE

function makeCounter(start = 0, step = 1) {
let count = start;
const history = [];

return {
increment() {
count += step;
history.push(count);
return this;
},
decrement() {
count -= step;
history.push(count);
return this;
},
getHistory: () => history,
getCount: () => count,
};
}

const counter = makeCounter(10, 3);
counter.increment().increment().decrement();

console.log(counter.getCount());
console.log(counter.getHistory());
  • ❤ 5
  • 👍 3
  • 🔥 2
Post #3321 2.61K
🤖 Eve: A Next.js-Style Framework for Building Agents

A new framework from Vercel that provides Next.js-esque structure for building AI-powered agents using TypeScript and Markdown. It's quite Vercel-flavored by default, but I found you can run it entirely independently of Vercel with a few settings tweaks and your own keys. Project homepage.

Vercel
  • 👍 8
  • ❤ 7
  • 🔥 4
Post #3320 2.7K
🌲 Node.js 26.4 Adds Package Maps

A minor release whose headline feature is the (experimental) implementation of package maps (which let Node resolve packages from a static JSON file rather than walking node_modules). Matteo Collina’s node:vfs subsystem also begins to make an appearance.

Antoine du Hamel
  • ❤ 5
  • 👍 2
  • 🔥 2
Post #3319 2.57K
  • ❤ 2
  • 🤔 2
  • 🔥 1
Post #3318 2.58K
CHALLENGE


class EventEmitter {
#listeners = new Map();

on(event, listener) {
if (!this.#listeners.has(event)) {
this.#listeners.set(event, []);
}
this.#listeners.get(event).push(listener);
return this;
}

emit(event, ...args) {
const handlers = this.#listeners.get(event) ?? [];
handlers.forEach(fn => fn(...args));
return this;
}

once(event, listener) {
const wrapper = (...args) => {
listener(...args);
this.off(event, wrapper);
};
return this.on(event, wrapper);
}

off(event, listener) {
const updated = (this.#listeners.get(event) ?? []).filter(fn => fn !== listener);
this.#listeners.set(event, updated);
return this;
}
}

const emitter = new EventEmitter();
const log = [];

emitter.on("data", val => log.push(`on:${val}`));
emitter.once("data", val => log.push(`once:${val}`));
emitter.on("data", val => log.push(`on2:${val}`));

emitter.emit("data", "A");
emitter.emit("data", "B");

console.log(log.join(", "));
  • 🔥 5
  • ❤ 4
  • 👍 4
Post #3316 2.29K
  • ❤ 3
  • 🔥 1
Post #3315 2.37K
CHALLENGE

class BankAccount {
#balance;
#transactionHistory = [];

constructor(initialBalance) {
this.#balance = initialBalance;
}

deposit(amount) {
this.#balance += amount;
this.#transactionHistory.push(`+${amount}`);
return this;
}

withdraw(amount) {
this.#balance -= amount;
this.#transactionHistory.push(`-${amount}`);
return this;
}

get summary() {
return `Balance: ${this.#balance} | Transactions: ${this.#transactionHistory.join(", ")}`;
}

static hasPrivateBalance(obj) {
return #balance in obj;
}
}

const account = new BankAccount(100);
account.deposit(50).deposit(25).withdraw(30);

console.log(account.summary);
console.log(BankAccount.hasPrivateBalance(account));
console.log(BankAccount.hasPrivateBalance({}));
  • ❤ 7
  • 🔥 5
  • 🤔 1
Post #3314 2.44K
  • 👍 3
  • ❤ 2
  • 🤔 1
Post #3313 2.18K
CHALLENGE

console.log('1: start');

setTimeout(() => console.log('2: setTimeout'), 0);

Promise.resolve()
.then(() => {
console.log('3: promise 1');
return Promise.resolve('chained');
})
.then((val) => console.log(`4: ${val}`));

queueMicrotask(() => console.log('5: microtask'));

new Promise((resolve) => {
console.log('6: executor');
resolve();
}).then(() => console.log('7: promise 2'));

console.log('8: end');
  • ❤ 3
  • 🔥 3
  • 👍 1
Post #3312 2.47K
  • 👍 3
  • ❤ 2
Post #3311 2.33K
CHALLENGE

const balance = 9007199254740991n; // Number.MAX_SAFE_INTEGER as BigInt
const fee = 1n;
const multiplier = 2n;

const total = (balance + fee) * multiplier;
const asNumber = Number(total);

const isSafe = Number.isSafeInteger(asNumber);
const isExact = BigInt(asNumber) === total;

console.log(`${total}n | safe: ${isSafe} | exact: ${isExact}`);
  • ❤ 3
  • 👍 2
  • 🔥 1
Post #3310 2.44K
  • ❤ 6
  • 👍 1
Post #3309 2.54K
CHALLENGE


function Vehicle(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.describe = function () {
return `${this.year} ${this.make} ${this.model}`;
};
}

Vehicle.prototype.age = function (currentYear) {
return currentYear - this.year;
};

const car = new Vehicle("Toyota", "Supra", 1998);
const truck = new Vehicle("Ford", "Raptor", 2021);

console.log(car.describe());
console.log(truck.age(2025));
console.log(car.constructor === Vehicle);
console.log(Object.getPrototypeOf(car) === Vehicle.prototype);
  • ❤ 5
  • 👍 2
  • 🔥 2
Post #3307 2.33K
CHALLENGE

function* range(start, end) {
while (start < end) {
yield start++;
}
}

function* labeled(prefix, gen) {
for (const val of gen) {
yield `${prefix}:${val}`;
}
}

function* pipeline() {
yield* labeled("A", range(1, 4));
yield* labeled("B", range(10, 12));
}

const results = [...pipeline()];
console.log(results.length, results[0], results[results.length - 1]);
  • 🔥 3
  • 👍 1
Older posts →
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →