Tips For Using Async/Await – Write Better JavaScript!

Basic Async/Await

Things to note –
There are two parts to using async/await in your code.
First of all, we have the async keyword, which you put in front of a function declaration to turn it into an async function. An async function is a fun…


This content originally appeared on DEV Community and was authored by milindsoorya

Basic Async/Await

Things to note -
There are two parts to using async/await in your code.
First of all, we have the async keyword, which you put in front of a function declaration to turn it into an async function. An async function is a function that knows how to expect the possibility of the await keyword being used to invoke asynchronous code.

const loadData = async () => {
  const url = "https://jsonplaceholder.typicode.com/todos/1";
  const res = await fetch(url);
  const data = await res.json();
  console.log(data);
};

loadData();
// Console output
{
  completed: false,
  id: 1,
  title: "delectus aut autem",
  userId: 1
}

Async/Await with error handling

We can handle errors using a try catch block.

const loadData = async () => {
  try{
      const url = "https://jsonplaceholder.typicode.com/todos/1";
      const res = await fetch(url);
      const data = await res.json();
      console.log(data);
  } catch(err) {
    console.log(err)
  }
};

loadData();

Things to note - The above try-catch will only handle error while fetching data such as wrong syntax, wrong domain names, network error etc. For the situation in which we want to handle an error message from the API response code, we can use res.ok, It will give a Boolean with the value true if the response code is between 200 and 209.

const loadData = async () => {
  try{
      const url = "https://jsonplaceholder.typicode.com/todos/qwe1";
      const res = await fetch(url);
      if(res.ok){ 
        const data = await res.json();
        console.log(data);
      } else {
        console.log(res.status); // 404
      }
  } catch(err) {
    console.log(err)
  }
};

loadData();

// OUTPUT
// 404

Async function returns a promise

This is one of the traits of async functions — their return values are guaranteed to be converted to promises. To handle data returned from an async function we can use a then keyword to get the data.

const loadData = async () => {
  try{
    const url = "https://jsonplaceholder.typicode.com/todos/1";
    const res = await fetch(url);
    const data = await res.json();
    return data
  } catch(err) {
    console.log(err)
  }
};

const data = loadData().then(data => console.log(data));

? PRO TIP :
if you want to use an async-await to handle the returned data you can make use of an IIFE, but it is only available in Node 14.8 or higher.

// use an async IIFE
(async () => {
  const data = await loadData();
  console.log(data);
})();

await only works inside async functions within regular JavaScript code, however it can be used on its own with JavaScript modules.

Promise.all()

Promise.all() comes in handy when we want to call multiple API's. Using a traditional await method we have to wait for each request to be completed before going to the next request. The problem occurs when each request takes some time to complete, this can easily add up and make the experience slower. Using Promise.all() we can call each of these API's in parallel. (it is an oversimplification, for more details checkout this amazing article).

const loadData = async () => {
  try{
    const url1 = "https://jsonplaceholder.typicode.com/todos/1";
    const url2 = "https://jsonplaceholder.typicode.com/todos/2";
    const url3 = "https://jsonplaceholder.typicode.com/todos/3";
    const results = await Promise.all([
      fetch(url1),
      fetch(url2),
      fetch(url3)
    ]);
    const dataPromises = await results.map(result => result.json());
    const finalData = Promise.all(dataPromises);
    return finalData
  } catch(err) {
    console.log(err)
  }
};

const data = loadData().then(data => console.log(data));
// Console output
[{
  completed: false,
  id: 1,
  title: "delectus aut autem",
  userId: 1
}, {
  completed: false,
  id: 2,
  title: "quis ut nam facilis et officia qui",
  userId: 1
}, {
  completed: false,
  id: 3,
  title: "fugiat veniam minus",
  userId: 1
}]

?? checkout my website, milindsoorya.site for more updates and getting in touch. cheers.

Thank you very much for reading, liking and commenting on my articles. If you have enjoyed my article or if it was helpful please support me by buying me a coffee ☕ ?.


This content originally appeared on DEV Community and was authored by milindsoorya


Print Share Comment Cite Upload Translate Updates
APA

milindsoorya | Sciencx (2021-06-12T08:55:52+00:00) Tips For Using Async/Await – Write Better JavaScript!. Retrieved from https://www.scien.cx/2021/06/12/tips-for-using-async-await-write-better-javascript/

MLA
" » Tips For Using Async/Await – Write Better JavaScript!." milindsoorya | Sciencx - Saturday June 12, 2021, https://www.scien.cx/2021/06/12/tips-for-using-async-await-write-better-javascript/
HARVARD
milindsoorya | Sciencx Saturday June 12, 2021 » Tips For Using Async/Await – Write Better JavaScript!., viewed ,<https://www.scien.cx/2021/06/12/tips-for-using-async-await-write-better-javascript/>
VANCOUVER
milindsoorya | Sciencx - » Tips For Using Async/Await – Write Better JavaScript!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/12/tips-for-using-async-await-write-better-javascript/
CHICAGO
" » Tips For Using Async/Await – Write Better JavaScript!." milindsoorya | Sciencx - Accessed . https://www.scien.cx/2021/06/12/tips-for-using-async-await-write-better-javascript/
IEEE
" » Tips For Using Async/Await – Write Better JavaScript!." milindsoorya | Sciencx [Online]. Available: https://www.scien.cx/2021/06/12/tips-for-using-async-await-write-better-javascript/. [Accessed: ]
rf:citation
» Tips For Using Async/Await – Write Better JavaScript! | milindsoorya | Sciencx | https://www.scien.cx/2021/06/12/tips-for-using-async-await-write-better-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.