All bites
The whole library, newest first. Filter by what you are here for, or pick a topic if you already know.
4247 bites
Page 74
Node.js Callbacks: Functions That Run Later
A callback is a function you pass to run later when an event fires or work finishes, keeping Node.js free to handle other work. HTTP servers use them to respond to connections without blocking.

JavaScript Promises: Handling Future Values
A Promise is an IOU for a future value from an async operation. Instead of blocking your code, you get an object that will eventually contain the result or an error. They're essential for API calls or file reads.

Promise.then(): Each Call Returns a New Promise
Each .then() call returns a new promise, letting you chain async tasks sequentially. This is key for multi-step operations like API calls. The footgun is attaching multiple .then()s to the original promise, which executes them in parallel, not in sequence.

Promise .catch(): Handling Rejections
.catch() is the try...catch for promises, intercepting errors (rejections) in a chain. Use it at the end of a promise chain to handle failures from preceding steps, like a failed API call. The footgun: placing it mid-chain can swallow errors.
Node.js util.promisify: From Callbacks to Promises
util.promisify converts callback-based functions into Promise-based ones, letting you use async/await with older Node.js APIs. It's a bridge for legacy code following the standard (err, value) callback pattern. The footgun: it fails on non-standard signatures.

Async/Await: Write Non-Blocking Code That Reads Synchronously
async/await lets you write non-blocking code that reads like simple, synchronous logic. It's used for network requests or database queries without freezing your app. The biggest footgun is using await inside a function you forgot to declare as async.

Promise.all(): Wait for Multiple Promises at Once
Promise.all() runs multiple promises in parallel, resolving only when all have succeeded. It's for when you need data from several API endpoints to render a single component.

Promise.race(): First Promise to Settle Wins
Promise.race() returns a promise that mirrors the outcome of the first promise in a set to finish—the winner takes all, whether it resolves or rejects. Use it to set a timeout on a network request.

Promise.allSettled(): Never Fail a Batch of Promises
Promise.allSettled() waits for every promise in a set to finish, success or fail, without short-circuiting. Use it for independent tasks, like multiple API calls, where you need the outcome of each.

Promise.any(): Get the Fastest Successful Result
Promise.any() is a race where only finishers count. It returns the value of the first promise to succeed, ignoring any that fail. Use it to query redundant endpoints and take the first successful response.

Top-Level Await: `await` Without an `async` Function
Top-level await lets you use await directly in an ES module, no async function needed. Use it to initialize resources like database connections on startup. The footgun: the entire module's execution blocks until the promise resolves, delaying startup.
Creating a Basic HTTP Server in Node.js
A Node.js HTTP server is a listening post that waits for requests on a port and runs your code to reply. It's the foundation for any web service, from simple APIs to full apps. The footgun: forgetting response.end() leaves the client hanging indefinitely.
Node.js File I/O: Synchronous vs. Asynchronous
Synchronous file I/O blocks your app, like waiting at a counter for your order. Asynchronous I/O gets a buzzer, letting your app work on other tasks. Use async for servers and sync for simple, one-off scripts. The footgun is using sync I/O in a server.
__dirname and __filename: Path Anchors in Node.js
__dirname and __filename act as GPS for your Node.js files, giving you the absolute path to the current file and its directory. This is essential for reliably loading adjacent files, like templates or configs. The main gotcha: they don't exist in ES Modules.
Node.js os Module: Reading Your System's Vital Signs
The Node.js os module is your app's dashboard for the host machine's vitals. Use it to check CPU cores, free memory, or platform type to adapt your code. The footgun is assuming consistency; system details can vary wildly between environments.
path.join() vs. path.resolve(): Concatenation vs. Calculation
path.join() glues path segments together, while path.resolve() calculates an absolute path. Think string building vs. cd commands. Use join for relative paths and resolve for absolute ones.
WHATWG URL API: Safely Parse URLs, Not Strings
Treat URLs as structured objects, not messy strings. The WHATWG URL API parses a URL into components like protocol and path, just like JSON.parse. Use it for incoming requests or outgoing API calls. The footgun is using the legacy url.parse().
Hashing Data with Node.js's `crypto` Module
Hashing creates a unique, fixed-size fingerprint of data. It's a one-way process used to verify data integrity or store passwords securely without saving the plain text. The footgun is using weak algorithms like MD5 or SHA1 for security-sensitive tasks.
Node's zlib Module: Trading CPU for Bandwidth
Node's zlib module trades CPU cycles for network bandwidth by shrinking data with algorithms like Gzip and Brotli. Use it to compress large API responses or files before sending them. The main footgun: never use synchronous ...Sync methods in a server.
Node.js DNS: lookup vs. resolve
Node.js splits DNS into two paths: dns.lookup uses getaddrinfo for IPs, while the dns.resolve family fetches records like MX or TXT. Use lookup for connections and the resolve family for service discovery.