2022-02-21

Asynchronous JavaScript

Asynchronous JavaScript

JavaScript is a single-threaded language, which means it can only handle one task at a time. However, certain operations like fetching data from a server, reading a file, or querying a database can take a significant amount of time to complete. This is where asynchronous JavaScript comes into play. It allows the JavaScript engine to execute other tasks while waiting for these operations to finish.

In synchronous programming, each task is executed one after the other. This means that a task must be completed before the next one starts. Asynchronous programming, on the other hand, allows tasks to be executed concurrently. This means that long-running tasks can be started and allowed to complete in the background while the main program continues to run.

Callbacks in JavaScript

In the world of JavaScript, functions play a pivotal role in crafting the behavior of applications. Their versatile nature allows them to be treated as first-class objects, meaning that they can be passed around like any other value in the language. A callback, in particular, is a function that is passed as an argument to another function and is expected to be executed at a later time.

The ability to pass functions around as values and have them executed later opens the door to a wide range of possibilities. Callbacks can be used to provide a custom behavior that should be executed after an operation has completed, or when a certain condition is met.

Implementing Callbacks in JavaScript

Let's consider a simple example. Suppose you have a function that takes a while to compute a result, like fetching data from a database. You don't want your program to wait and do nothing while this function is executing, so you use a callback.

Here's what the code might look like:

js
function fetchData(callback) {
  // Simulate a delay with setTimeout
  setTimeout(function() {
    let data = 'Hello, world!';
    callback(data);
  }, 2000);
}

function printData(data) {
  console.log(data);
}

// Call fetchData and pass in printData as the callback
fetchData(printData);

In this example, fetchData takes a callback function as an argument. It simulates fetching data with a delay using setTimeout. When the data is ready, it calls the callback function with the data as an argument. The printData function is passed as the callback, so it gets called when the data is ready, and it prints the data to the console.

Problem with Callback Hell

Callbacks are great for simple cases, but things can get complicated when you start to nest callbacks within callbacks. This situation is commonly referred to as "callback hell".

For instance, if you need to fetch data from the database, then based on that data fetch some more data, and then based on that data do something else, you'll end up with callbacks nested within callbacks.

js
fetchData(function(data) {
  fetchMoreData(data, function(moreData) {
    doSomethingWithTheData(moreData, function(result) {
      console.log(result);
    });
  });
});

The code starts to look like a pyramid, and it can quickly become hard to read and maintain. This is one of the main problems that Promises and Async/Await aim to solve.

Promises in JavaScript

As a response to the challenges posed by callbacks, particularly "callback hell," Promises in JavaScript were introduced. Promises are objects that represent the eventual completion or failure of an asynchronous operation. They provide a robust way to handle asynchronous operations and avoid deep nesting of callbacks.

Promise States and Lifecycle

A Promise can be in one of three states:

  • Pending
    The promise’s outcome hasn’t yet been determined, because the asynchronous operation that will produce its result hasn’t completed yet.
  • Fulfilled
    The asynchronous operation has completed, and the promise has a resulting value.
  • Rejected
    The asynchronous operation failed, and the promise will never be fulfilled. In the rejected state, a promise has a reason that indicates why the operation failed.

Importantly, once a promise is fulfilled or rejected, its state cannot change. The promise is said to be settled if it is either fulfilled or rejected.

Creating and Consuming Promises

Promises are created using the new Promise constructor. The Promise constructor takes a function, called the executor, that takes two arguments: resolve and reject. The resolve and reject arguments are functions that, when called, settle the promise. resolve fulfills the promise with a value, and reject rejects the promise with a reason.

Here's a simple example of creating a promise:

js
let myFirstPromise = new Promise((resolve, reject) => {
  let condition = true; // this could be the result of some asynchronous operation

  if(condition) {
    resolve('Promise is fulfilled!');
  } else {
    reject('Promise is rejected!');
  }
});

To consume a promise, you can use its then method. The then method takes two optional arguments: a callback for a success case and another for a failure case. These callbacks are also known as handlers.

Here's an example of consuming the promise we created above:

js
myFirstPromise
  .then(successMessage => {
    console.log(successMessage);
  })
  .catch(errorMessage => {
    console.error(errorMessage);
  });

In this example, if the promise is fulfilled, the success message is logged to the console. If the promise is rejected, the error message is logged to the console.

Chaining Promises

One of the most powerful features of promises is the ability to chain them together. This means that you can perform multiple asynchronous operations back to back where each subsequent operation starts when the previous one has completed.

Here's an example of promise chaining:

js
fetchData()
  .then(data => {
    console.log(data);
    return fetchMoreData(data);
  })
  .then(moreData => {
    console.log(moreData);
    return doSomethingWithTheData(moreData);
  })
  .then(result => {
    console.log(result);
  })
  .catch(error => {
    console.error(error);
  });

In this example, each then returns a new promise, which forms a chain of promises. If any promise in the chain is rejected, the control jumps to the nearest catch method. This makes error handling in promise chains very straightforward.

Async/Await

Async/Await is a modern way of handling asynchronous operations in JavaScript. Introduced with ES2017, Async/Await is essentially syntactic sugar built on top of promises, which allows us to work with promises in a more pleasing and readable manner.

Async/Await makes asynchronous code look and behave more like synchronous code, which makes it easier to understand and reason about. It's a powerful tool for writing asynchronous JavaScript code that's clean, concise, and easy to read.

Using Async/Await in JavaScript

To use Async/Await, you start by defining an asynchronous function using the async keyword. Inside an async function, you can use the await keyword before a promise to pause the execution of the async function and wait for the promise to resolve or reject.

Here's an example:

js
async function fetchAndDisplayData() {
  try {
    let data = await fetchData();
    console.log(data);

    let moreData = await fetchMoreData(data);
    console.log(moreData);

    let result = await doSomethingWithTheData(moreData);
    console.log(result);
  } catch(error) {
    console.error(error);
  }
}

fetchAndDisplayData();

In this example, fetchData, fetchMoreData, and doSomethingWithTheData are all functions that return promises. The await keyword before these function calls makes JavaScript wait until the promise settles and then return its result. This means that fetchAndDisplayData behaves as if it's a synchronous function, even though it contains asynchronous operations.

Error Handling in Async/Await

Error handling in async functions is done using try/catch blocks, just like in synchronous code. In the example above, if any of the promises is rejected, the execution jumps to the catch block.

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!