Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active October 4, 2024 19:06
Show Gist options
  • Select an option

  • Save wilmoore/acffb39be382bd1dc710084e3fdc6f1d to your computer and use it in GitHub Desktop.

Select an option

Save wilmoore/acffb39be382bd1dc710084e3fdc6f1d to your computer and use it in GitHub Desktop.
Software Engineering :: Programming :: Languages :: JavaScript :: Asynchronicity :: Guide

Software Engineering :: Programming :: Languages :: JavaScript :: Asynchronicity :: Guide

⪼ Made with 💜 by Polyglot.

related

The JavaScript Asynchronicity Guide

In JavaScript, asynchronous operations are essential for performing tasks that might take some time to complete without blocking the main thread. This is particularly important in web development, where you don't want to freeze the user interface while waiting for actions like fetching data from a server or reading files. Here are the primary asynchronous features in JavaScript:

Understanding and effectively using these asynchronous features are crucial for developing responsive and efficient web applications in JavaScript.

  • Callbacks: Functions that are passed as arguments to other functions and are executed after some operation completes. They were the earliest way to handle asynchronous operations in JavaScript.
  • Promises: Objects that represent the eventual completion (or failure) of an asynchronous operation and its resulting value. Promises allow for more manageable asynchronous code, avoiding the so-called "callback hell."
  • async/await: A syntactical feature introduced in ES2017 to work with Promises in a more synchronous-looking manner. The async keyword is used to declare an asynchronous function, and await is used to wait for a Promise to resolve.
  • Event Listeners: Functions that are called in response to certain events (like clicks, key presses, or data loading). Event listeners can be set up to respond asynchronously when an event occurs.
  • SetTimeout/SetInterval: Functions for setting timers. setTimeout allows you to execute a function once after a specified time, and setInterval executes a function repeatedly at specified intervals. Both are asynchronous and don't block the main thread while waiting.
  • Web Workers: A way to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface. This is particularly useful for performing expensive computations.
  • Fetch API: Provides a more powerful and flexible feature to make network requests. The Fetch API returns Promises and is often used in modern web applications for asynchronous HTTP requests.
  • Streams: Allow you to process data in chunks as it comes in, which is useful for tasks like parsing large files or handling data in real-time.
  • IndexedDB: A low-level API for client-side storage that is asynchronous. It allows you to create, read, navigate, and write to a user's browser database on a background thread.
  • Promises combined with other features: Such as Promise.all, Promise.race, Promise.allSettled, and Promise.any. These methods are for handling multiple promises together in different ways.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment