This blog post shows how to use promises in JavaScript to enable non-blocking async tasks. It breaks down the process from start to finish. Promises can trigger an asynchronous action if a handler is attached by either then or catch. Since the handlers are pushed to the Microtask Queue, you can handle the eventual result in a non-blocking way. This makes it easier to handle errors, chain multiple operations together, and keep your code more readable.
Wednesday, April 3, 2024Promises in JavaScript aren't always intuitive to use, but they're required to be productive with JavaScript. Promises are needed because JavaScript is single-threaded, which can cause issues when blocking operations like window.prompt() are used. This post dives into the mechanics of Promises, their states (pending, fulfilled, rejected), and how to work with them using the .then() and .catch() methods. It also covers creating custom Promises, chaining them together, passing data between them, and the modern async/await syntax for a more synchronous-like experience.
JavaScript doesn't natively support canceling Promises. You can use `Promise.withResolvers()` to create cancelable tasks by manually resolving or rejecting Promises. Alternatively, `AbortController` can handle early rejection, which can be used for cancelable fetch requests and sequential request handling in React.
This article explores how to simulate millions of particles in JavaScript, achieving a smooth 60 frames per second using only the CPU, particularly focusing on optimization for mobile devices. The author details their process, which involved several iterations, starting with a basic multi-threaded approach using web workers and SharedArrayBuffers. They faced challenges like flickering and performance bottlenecks in rendering and data transfer. To improve performance, they implemented strategies like double buffering and a particle grid count.
- JavaScript's garbage collection can lead to memory leaks due to retained global variable references.
JavaScript garbage collection doesn't always release memory when a function is no longer callable, especially if the function's scope contains references to global variables, leading to memory leaks.
JavaScript's WeakMap should be made iterable, as the original motivation for its non-iterability is no longer valid and the current workaround using FinalizationRegistry is inefficient and non-standard.
The Temporal API in JavaScript offers a new approach to handling time with the Temporal.ZonedDateTime object, which accurately represents dates and times with their corresponding time zones. This addresses the limitations of JavaScript's Date object, which doesn't capture the nuances of human-readable dates and the impact of time zones and daylight savings time. The new API introduces a standardized format for representing zoned dates and times, allowing developers to work with different calendars and time zones easily.
- Understanding the role and benefits of JavaScript Generators in data processing and task management.
Generators in JavaScript are special functions that allow you to pause and resume execution, making them ideal for controlling when data is processed. They are especially useful for handling large datasets, asynchronous tasks, and complex iteration patterns.