Answers for "how to write async await function in javascript"

4

javascript async function

// Old School Javascript Invoke
(async function() {
	await someAsyncFunction();
})();

//New ES6 Javascript Invoke
(async () => {
	await someAsyncFunction();
})();
Posted by: Guest on September-26-2021
9

how to make an async function

function resolveAfter2Seconds() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, 2000);
  });
}

//async function:
async function asyncCall() {
  console.log('calling');
  const result = await resolveAfter2Seconds();
  console.log(result);
  // expected output: 'resolved'
}

asyncCall();
Posted by: Guest on March-03-2020
0

async await

//used in node.js
const fetch = require('node-fetch');
async function Showuser() {
  const result = await fetch('https://jsonplaceholder.typicode.com/todos/1');
  const date = await result.json();
  console.log(date);
}

Showuser();
Posted by: Guest on August-28-2021
3

async

function resolveAfter2Seconds() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, 2000);
  });
}

async function asyncCall() {
  console.log('calling');
  const result = await resolveAfter2Seconds();
  console.log(result);
  // expected output: 'resolved'
}

asyncCall();
Posted by: Guest on May-29-2020
0

async await

// ASYNC will always returns promises
// NOTE : AWAIT should be kept only inside ASYNC function
// AWAIT can't be used in regular function
/* TIPS : Js is single threaded & synchronous in nature BUT, we can
          make it as asyncronous by using (ASYNC/AWAIT)*/

//(Example 1 : fetching Random Image)
async function RandomImage(){  //remember async and await is powerful for async operations, always await should be inside of async only.

  try {
    const raw_response = await fetch("https://www.themealdb.com/api/json/v1/1/random.php");
      if (!raw_response.ok) { // check for the 404 errors
          throw new Error(raw_response.status);
      }
    const json_data = await raw_response.json();  //AWAIT
    let data = json_data.meals[0];

    console.log(data);
  }
  catch (error) { // catch block for network errors
      console.log(error); 
  }
}
RandomImage();


//(Example 2 : returning another promise)
console.log("1 is working");
console.log("2 is working");
  var AsyncFunction = async() => {
    var x = new Promise((resolve,reject) => {
        setTimeout(() => resolve("3 is working"), 3000);
    });
    var result = await x;
    return result;
  }
AsyncFunction().then(resolved => console.log(resolved));
console.log("3 is working");
Posted by: Guest on December-16-2020
0

javascript async await

// The await operator in JavaScript can only be used from inside an async function.
// If the parameter is a promise, execution of the async function will resume when the promise is resolved
// (unless the promise is rejected, in which case an error will be thrown that can be handled with normal JavaScript exception handling).
// If the parameter is not a promise, the parameter itself will be returned immediately.[13]

// Many libraries provide promise objects that can also be used with await,
// as long as they match the specification for native JavaScript promises.
// However, promises from the jQuery library were not Promises/A+ compatible until jQuery 3.0.[14]

async function createNewDoc() {
  let response = await db.post({}); // post a new doc
  return await db.get(response.id); // find by id
}

async function main() {
  try {
    let doc = await createNewDoc();
    console.log(doc);
  } catch (err) {
    console.log(err);
  }
}
main();
Posted by: Guest on April-22-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language