1const data = async ()  => {
2  const got = await fetch('https://jsonplaceholder.typicode.com/todos/1');
3  
4  console.log(await got.json())
5}
6
7data();1async function showAvatar() {
2
3  // read our JSON
4  let response = await fetch('/article/promise-chaining/user.json');
5  let user = await response.json();
6
7  // read github user
8  let githubResponse = await fetch(`https://api.github.com/users/${user.name}`);
9  let githubUser = await githubResponse.json();
10
11  // show the avatar
12  let img = document.createElement('img');
13  img.src = githubUser.avatar_url;
14  img.className = "promise-avatar-example";
15  document.body.append(img);
16
17  // wait 3 seconds
18  await new Promise((resolve, reject) => setTimeout(resolve, 3000));
19
20  img.remove();
21
22  return githubUser;
23}
24
25showAvatar();1async function f() {
2
3  try {
4    let response = await fetch('/no-user-here');
5    let user = await response.json();
6  } catch(err) {
7    // catches errors both in fetch and response.json
8    alert(err);
9  }
10}
11
12f();1async function showAvatar() {
2	// read
3  	await setTimeout(resolve, 3000);
4    // read next 3s
5}
6
7showAvatar();1//used in node.js
2const fetch = require('node-fetch');
3async function Showuser() {
4  const result = await fetch('https://jsonplaceholder.typicode.com/todos/1');
5  const date = await result.json();
6  console.log(date);
7}
8
9Showuser();1// ASYNC will always returns promises
2// NOTE : AWAIT should be kept only inside ASYNC function
3// AWAIT can't be used in regular function
4/* TIPS : Js is single threaded & synchronous in nature BUT, we can
5          make it as asyncronous by using (ASYNC/AWAIT)*/
6
7//(Example 1 : fetching Random Image)
8async function RandomImage(){  //remember async and await is powerful for async operations, always await should be inside of async only.
9
10  try {
11    const raw_response = await fetch("https://www.themealdb.com/api/json/v1/1/random.php");
12      if (!raw_response.ok) { // check for the 404 errors
13          throw new Error(raw_response.status);
14      }
15    const json_data = await raw_response.json();  //AWAIT
16    let data = json_data.meals[0];
17
18    console.log(data);
19  }
20  catch (error) { // catch block for network errors
21      console.log(error); 
22  }
23}
24RandomImage();
25
26
27//(Example 2 : returning another promise)
28console.log("1 is working");
29console.log("2 is working");
30  var AsyncFunction = async() => {
31    var x = new Promise((resolve,reject) => {
32        setTimeout(() => resolve("3 is working"), 3000);
33    });
34    var result = await x;
35    return result;
36  }
37AsyncFunction().then(resolved => console.log(resolved));
38console.log("3 is working");1async function () {
2  const fetchAPI = fetch(`https://bn-hadith-api.herokuapp.com/hadiths/0`);
3  const response = await fetchAPI;
4  const data = await response.json();
5  console.log(data);
6}
7