Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Async / Await

Async Syntax

Placing the async keyword before a function makes the function return a Promise.

Example

async function myFunction() {
  return “Hello”;
}

Is the same as:

function myFunction() {
  return Promise.resolve(“Hello”);
}

This is how you can use a Promise:

myFunction().then(
  function(value) { /* code if successful */ },
  function(error) { /* code if some error */ }
);

Example

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

Or more simply, if you expect a standard value (a normal response, not an error):

Example

async function myFunction() {
  return “Hello”;
}
myFunction().then(
  function(value) {myDisplayer(value);}
);