js- Asyn and await

// we need .then to access data returned from promise function 


/* async and await : both return pormise , so controll will allow them to function in backgroud , do the rest of the task and then come back to them */


async function harry(){

  console.log("inside harry function")

  const response = await fetch("https://api.github.com/users");

  console.log('before response')

  const users = await response.json();

  return users;

}

console.log("before fun call")

a=harry()

console.log("after func call");

console.log(a)

a.then((data) => {

  console.log(data)

})

console.log("last line of java script")


/* 

output :

------------

before fun call

index.js:6 inside harry function

index.js:15 after func call

index.js:16 Promise {<pending>}

index.js:20 last line of java script

index.js:8 before response

index.js:18 (30) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]


*/


0 Comments