Delving Developer

async/await vs. Promises - Which should I use?

Eddie Cunningham
Eddie Cunningham
3 min readJavascript
Cover Image for async/await vs. Promises - Which should I use?

The async/await keywords are a relatively new addition to Javascript, and they are often used in conjunction with Promises to provide a more convenient and readable way of handling asynchronous operations. However, some important differences between the two should be understood to use them effectively.

What is async/await?link

Async/await is a syntax that allows for asynchronous operations to be written in a more synchronous-looking style. This can make code that would otherwise be difficult to read and understand much more manageable. Please look at the example below for syntax:

async function fetchData() {
  const response = await fetch('https://example.com/data');
  const data = await response.json();
  return data;
}

What is a Promise?link

A Promise is an object that represents the eventual result of an asynchronous operation. Promises can be used to chain together multiple asynchronous operations, and they provide a way to handle errors that may occur during those operations. The following is an example of a function using Promises:

function fetchData() {
  return fetch('https://example.com/data')
    .then(response => response.json())
}

The difference between async/await and Promiseslink

Firstly, async functions always return a Promise, even if the operation they are performing is synchronous. This means that you can always use .then() and .catch() methods on the return value of an async function, even if you are not using Promises explicitly.

Secondly, await can only be used inside an async function. It causes the function to pause execution until the Promise that it is waiting on is resolved and then returns the result of that Promise. This can be useful for making sure that asynchronous operations are completed in the correct order, but it also means that you need to be careful about error handling, as any errors that occur inside an async function will be wrapped in a Promise and will be handled by the .catch() method.

Finally, while Promises can be used inside an async function, they can also be used independently of async/await. This means that you can use Promises to write code that is compatible with older versions of Javascript or to use alternative libraries that provide Promise-like functionality.

Which one should you use?link

There is no definitive answer to this question, as it depends on your personal preferences and the specific situation that you are working in. However, there are some general guidelines that you can follow.

If you need to perform a series of asynchronous operations and you want to make sure that they are completed in a specific order, then async/await is probably the best option. This is because you can use the await keyword to pause the execution of the function until the Promise that you are waiting on is resolved.

If you are working with code that is already using Promises, then you may want to continue using Promises rather than switching to async/await. This is because Promises are a well-established way of handling asynchronous operations, and they are compatible with a wide range of existing code.

Finally, if you are starting a new project from scratch, then you may want to consider using async/await, as it can make your code simpler and more readable.