Lädt...


🔧 Asynchronous JavaScript: Promises, Async/Await, and Callbacks


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Originally published on Makemychance.com

Asynchronous programming is a core concept in JavaScript, which allows you to perform tasks without blocking the execution of other code. This becomes especially important when dealing with operations that take time to complete, such as network requests, file I/O, or timers. In this article, we will explore the three main techniques for handling asynchronous code in JavaScript: Callbacks, Promises, and Async/Await.

1. Callbacks

Callbacks are the oldest way of handling asynchronous operations in JavaScript. A callback is simply a function passed as an argument to another function, which is then executed after the completion of a task.


function fetchData(callback) {
  setTimeout(() => {
    callback("Data received");
  }, 2000);
}

fetchData((message) => {
  console.log(message);
});

In the example above, fetchData simulates a network request with setTimeout, and the callback function logs the message after the request is completed.

Callback Hell

One of the downsides of using callbacks is the infamous “callback hell” or “pyramid of doom,” where multiple nested callbacks make the code difficult to read and maintain.

fetchData((message) => {
  console.log(message);
  fetchMoreData((moreData) => {
    console.log(moreData);
    fetchEvenMoreData((evenMoreData) => {
      console.log(evenMoreData);
      // And so on...
    });
  });
});

2. Promises

Promises, introduced in ES6, offer a cleaner approach to handling asynchronous tasks, helping to overcome the challenges of deeply nested callbacks. Essentially, a promise is an object that symbolizes the outcome of an asynchronous operation, whether it successfully completes or fails, and it provides a structured way to handle the resulting value.

function fetchData() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve("Data received");
    }, 2000);
  });
}

fetchData()
  .then((message) => {
    console.log(message);
    return "Next step";
  })
  .then((nextMessage) => {
    console.log(nextMessage);
  })
  .catch((error) => {
    console.error("Error:", error);
  });

In this example, fetchData returns a promise. The .then() method is used to handle the resolved value of the promise, and .catch() is used to handle any errors.

Chaining Promises

Promises can be chained, making the code more readable and maintainable.

fetchData()
  .then((message) => {
    console.log(message);
    return fetchMoreData();
  })
  .then((moreData) => {
    console.log(moreData);
    return fetchEvenMoreData();
  })
  .then((evenMoreData) => {
    console.log(evenMoreData);
  })
  .catch((error) => {
    console.error("Error:", error);
  });

3. Async/Await

Async/Await, introduced in ES8 (2017), is a syntactic sugar built on top of promises, making asynchronous code look and behave more like synchronous code.

async function fetchData() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve("Data received");
    }, 2000);
  });
}

async function processData() {
  try {
    const message = await fetchData();
    console.log(message);
    const moreData = await fetchMoreData();
    console.log(moreData);
    const evenMoreData = await fetchEvenMoreData();
    console.log(evenMoreData);
  } catch (error) {
    console.error("Error:", error);
  }
}

processData();

In this example, the processData function uses the await keyword to wait for the promise returned by fetchData to resolve. This makes the code much cleaner and easier to follow compared to promise chaining.

Error Handling

Error handling in async/await is done using try...catch blocks, providing a straightforward way to handle errors without the need for a .catch() method.


async function processData() {
  try {
    const data = await fetchData();
    console.log(data);
  } catch (error) {
    console.error("Error:", error);
  }
}
...

🔧 Understand the Asynchronous JavaScript: Callbacks, Promises, and Async/Await


📈 89.03 Punkte
🔧 Programmierung

🔧 Exploring Asynchronous JavaScript: Callbacks, Promises, and Async/Await


📈 89.03 Punkte
🔧 Programmierung

🔧 Asynchronous JavaScript: Callbacks, Promises, and Async/Await


📈 89.03 Punkte
🔧 Programmierung

🔧 Mastering Asynchronous JavaScript: Promises, Async/Await, and Callbacks


📈 89.03 Punkte
🔧 Programmierung

🔧 Understanding Asynchronous JavaScript: Callbacks, Promises, and Async/Await


📈 89.03 Punkte
🔧 Programmierung

🔧 Asynchronous JavaScript: Promises, Async/Await, and Callbacks


📈 89.03 Punkte
🔧 Programmierung

🔧 🌀 Understanding Asynchronous JavaScript: Callbacks, Promises, and Async/Await


📈 89.03 Punkte
🔧 Programmierung

🔧 Mastering Asynchronous JavaScript: Promises, Async/Await, and Callbacks 🚀


📈 89.03 Punkte
🔧 Programmierung

🔧 Async Made Easy: A Deep Dive into JavaScript Callbacks, Promises, and Async/Await


📈 88.23 Punkte
🔧 Programmierung

🔧 Async Made Easy: A Deep Dive into JavaScript Callbacks, Promises, and Async/Await


📈 88.23 Punkte
🔧 Programmierung

🔧 Asynchronous Programming in JavaScript – Callbacks, Promises, & Async/Await Examples


📈 87.4 Punkte
🔧 Programmierung

🎥 Asynchronous JavaScript Course – Async/Await , Promises, Callbacks, Fetch API


📈 87.4 Punkte
🎥 Video | Youtube

🔧 Asynchronous Programming in JavaScript: Callbacks vs Promises vs Async/Await


📈 87.4 Punkte
🔧 Programmierung

🔧 Asynchronous Programming in JavaScript - Callbacks | Promises | Async/Await


📈 87.4 Punkte
🔧 Programmierung

🔧 Asynchronous programming Callbacks, Promises & Async Await


📈 80.95 Punkte
🔧 Programmierung

🔧 Is async/await a good idea? 🤔 async/await vs promises


📈 77.37 Punkte
🔧 Programmierung

🔧 Unraveling the Mysteries of Asynchronous JavaScript: Callbacks to Async/Await


📈 75.22 Punkte
🔧 Programmierung

🔧 Mastering JavaScript Async Patterns: From Callbacks to Async/Await


📈 74.42 Punkte
🔧 Programmierung

🔧 Flow Control in JavaScript: Callbacks, Promises, async/await


📈 71.66 Punkte
🔧 Programmierung

🔧 Callbacks vs Promises vs Async/Await Concept in JavaScript


📈 71.66 Punkte
🔧 Programmierung

🔧 Mastering Asynchronous JavaScript: A Guide to async/await and Promises ⌛️


📈 68.59 Punkte
🔧 Programmierung

🔧 Mastering Asynchronous JavaScript: A Guide to async/await and Promises ⌛️


📈 68.59 Punkte
🔧 Programmierung

🔧 Mastering Asynchronous JavaScript: How to Effectively Use Promises, Async/Await, and Error Handling


📈 68.59 Punkte
🔧 Programmierung

🔧 Mastering Asynchronous JavaScript: A Guide to Promises and Async/Await


📈 68.59 Punkte
🔧 Programmierung

🔧 Asynchronous JavaScript: Promises Async Await!


📈 66.96 Punkte
🔧 Programmierung

🔧 Asynchronous JavaScript: Promises vs. Async/Await in Details


📈 66.96 Punkte
🔧 Programmierung

🔧 Callbacks, Promises, and Async-Await


📈 66.85 Punkte
🔧 Programmierung

🔧 Callbacks, Callback Hell, Promises, Async/Await


📈 65.22 Punkte
🔧 Programmierung

🔧 Callbacks, Callback Hell, Promises, Async/Await


📈 65.22 Punkte
🔧 Programmierung

🔧 Understanding Asynchronous Operations and Using async/await in JavaScript


📈 56.41 Punkte
🔧 Programmierung

🔧 Master Async/Await in JavaScript: A Practical Guide for Asynchronous Programming


📈 54.78 Punkte
🔧 Programmierung

🔧 Mastering Async Await in JavaScript for Asynchronous Programming


📈 54.78 Punkte
🔧 Programmierung

🔧 Mastering Async/Await: Simplifying JavaScript's Async Operations


📈 53.98 Punkte
🔧 Programmierung

🔧 Async/await at the speed of callbacks


📈 53.04 Punkte
🔧 Programmierung

🔧 Using Promises and Async/Await in JavaScript


📈 52.85 Punkte
🔧 Programmierung

matomo