JavaScript Await

Are you looking to learn more about JavaScript Await? This tutorial is designed for beginners and will provide an introduction to the concept of asynchronous programming in JavaScript. We'll go over the basics of JavaScript Await, including what it is, why you should use it, and how to use it in your code.

Why should you use it?

  • It allows you to write code that is more efficient and easier to read.
  • It helps to reduce the amount of time it takes for a program to execute.
  • It can help to improve the performance of your application.

Await

The 'await' keyword is used to wait for a Promise to resolve or reject. It can only be used inside an async function. The await keyword will pause the execution of the async function and wait for the Promise to resolve. Once the Promise is resolved, the async function will continue execution. The await keyword can be used to make asynchronous code look and behave like synchronous code. Consider the following example:
script.js
async function getData() {
  const data = await fetch('https://example.com/data');
  return data;
}

async function main() {
  const data = await getData();
  console.log(data);
}

main();
In the above example, the function 'getData()' returns a Promise. The await keyword is used to wait for the Promise to resolve. Once the Promise is resolved, the value of the Promise is assigned to the variable 'data'. The await keyword can also be used with multiple Promises. Consider the following example:
script.js
async function getData1() {
  const data1 = await fetch('https://example.com/data1');
  return data1;
}

async function getData2() {
  const data2 = await fetch('https://example.com/data2');
  return data2;
}

async function main() {
  const data1 = await getData1();
  const data2 = await getData2();
  console.log(data1, data2);
}

main();
In the above example, the await keyword is used to wait for both Promises to resolve. Once both Promises are resolved, the values are assigned to the variables 'data1' and 'data2'. The await keyword can also be used in a loop. Consider the following example:
script.js
async function getData(url) {
  const data = await fetch(url);
  return data;
}

async function main() {
  const urls = [
    'https://example.com/data1',
    'https://example.com/data2',
    'https://example.com/data3',
  ];

  const data = [];
  for (const url of urls) {
    const res = await getData(url);
    data.push(res);
  }

  console.log(data);
}

main();
In the above example, the await keyword is used in a loop to wait for each Promise to resolve. Once each Promise is resolved, the value is added to the array 'data'. The await keyword can also be used with error handling. Consider the following example:
script.js
async function getData() {
  const data = await fetch('https://example.com/data')
    .catch((err) => {
      console.error(err);
    });
  return data;
}

async function main() {
  const data = await getData();
  console.log(data);
}

main();
In the above example, the await keyword is used to wait for the Promise to resolve. If the Promise is rejected, the error is caught and handled.