# Agenda

  • AMA (15 min)
  • Asynchronous Code
    • Callbacks
    • Promises
    • Async/Await
  • Error handling
  • Logging

::: Reading from week 1 Asynchronous architecture (opens new window) :::

Asynchronous code is an essential aspect of modern web development, especially when using Node.js and Express. It allows you to perform multiple tasks at once, improving the responsiveness of your application and freeing up resources for other operations.

Before we dive into Async/Await let's first understand Callbacks and Promises in JavaScript.

# Callbacks

Callbacks are the simplest form of asynchronous programming in Node.js and Express. A callback is a function passed as an argument to another function, which is then executed at a later point in time. The function that receives the callback is referred to as the "parent" function, while the function that is passed as an argument is referred to as the "child" function.

Here's an example of using a callback in a Node.js/Express application:

app.get('/', (req, res) => {
  fs.readFile('data.txt', (err, data) => {
    if (err) throw err;
    res.send(data.toString());
  });
});

In this example, we use a callback in the fs.readFile method to read a file and return its contents to the client. The readFile method takes two arguments: the file path and the callback function. The callback function receives two arguments: an error object and the file data. If an error occurs, the err argument will be set, and we throw an error. If no error occurs, the data argument is set, and we send the file contents back to the client.

# Promises

Promises are a more advanced form of asynchronous programming in Node.js and Express, designed to address the drawbacks of callbacks. A Promise represents a value that may not be available yet but will be in the future. Promises provide a way to register callbacks to be called when the value is available, as well as catch errors that occur during the process.

Here's an example of using a Promise in a Node.js/Express application:

app.get('/', (req, res) => {
  let promise = new Promise((resolve, reject) => {
    fs.readFile('data.txt', (err, data) => {
      if (err) reject(err);
      resolve(data.toString());
    });
  });

  promise.then((data) => {
    res.send(data);
  }).catch((err) => {
    res.send(err);
  });
});

In this example, we use a Promise to read a file and return its contents to the client. We create a Promise using the Promise constructor and pass a callback function that takes two arguments: resolve and reject. The resolve function is called when the task is complete, and the reject function is called when an error occurs. We then use the then method to register a callback to be executed when the Promise is resolved and the catch method to handle any errors that occur during the process.

# Async/Await

Async/Await is the latest and most advanced form of asynchronous programming in Node.js and Express. It provides a way to write asynchronous code that looks and behaves like synchronous code, making it easier to understand and maintain.

Here's a simple example of using Async/Await in a Node.js/Express application:

app.get('/', async (req, res) => {
  try {
    let data = await new Promise((resolve, reject) => {
      fs.readFile('data.txt', (err, data) => {
        if (err) reject(err);
        resolve(data.toString());
      });
    });
    res.send(data);
  } catch (err) {
    res.send(err);
  }
});

In this example, we use the async keyword to declare an asynchronous function. The function contains a try/catch block that wraps the asynchronous code. Inside the try block, we use the await keyword to wait for a Promise to be resolved. In this case, we create a Promise using the Promise constructor, which reads a file and returns its contents as a string. If the Promise is resolved, the data is sent back to the client. If an error occurs, it is caught in the catch block and sent back to the client.

Async/Await provides a more elegant solution than callbacks or Promises, as it eliminates the need for nested callback functions and makes it easier to manage errors. It also allows you to write asynchronous code that is easier to understand, as it looks and behaves like synchronous code.

# More Resources to read on Async/Await:

Last Updated: 2/6/2023, 8:45:12 AM