You are here: Foswiki>ReactJs Web>ExploringECMAScript6(ES-6)>AsyncAndAwait (26 Jan 2022, shss\dhakchina)Edit Attach
-- Main.shssdhakchina - 26 Jan 2022

JavaScript Async & Await

"async and await make promises easier to write"

async makes a function return a Promise

await makes a function wait for a Promise

Example
async function myFunction() {
  return "Hello";
}
myFunction().then(
  function(value) {myDisplayer(value);},
  function(error) {myDisplayer(error);}
);

Await Syntax

The keyword await before a function makes the function wait for a promise:

let value = await promise;
The await keyword can only be used inside an async function.

async function myDisplay() {
  let myPromise = new Promise(function(resolve, reject) {
    resolve("I love You !!");
  });
  document.getElementById("demo").innerHTML = await myPromise;
}

myDisplay();

Topic revision: r1 - 26 Jan 2022, shss\dhakchina
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback