W3Schools.com (2024)

"async and await make promises easier to write"

async makes a function return a Promise

await makes a function wait for a Promise

Async Syntax

The keyword async 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");
}

Here is how to use the 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);}
);

Try it Yourself »

Or simpler, since you expect a normal value (a normal response, not an error):

Example

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

Try it Yourself »

Await Syntax

The await keyword can only be used inside anasync function.

The await keyword makes the function pause the execution and wait for a resolved promise before it continues:

let value = await promise;

Example

Let's go slowly and learn how to use it.

Basic Syntax

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

myDisplay();

Try it Yourself »

The two arguments (resolve and reject) are pre-defined by JavaScript.

We will not create them, but call one of them when the executor function is ready.

Very often we will not need a reject function.

Example without reject

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

myDisplay();

Try it Yourself »

Waiting for a Timeout

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

myDisplay();

Try it Yourself »

Waiting for a File

async function getFile() {
let myPromise = new Promise(function(resolve) {
let req = new XMLHttpRequest();
req.open('GET', "mycar.html");
req.onload = function() {
if (req.status == 200) {
resolve(req.response);
} else {
resolve("File not Found");
}
};
req.send();
});
document.getElementById("demo").innerHTML = await myPromise;
}

getFile();

Try it Yourself »

Browser Support

ECMAScript 2017 introduced the JavaScript keywordsasync and await.

The following table defines the first browser version with full support for both:

Chrome 55 Edge 15 Firefox 52 Safari 11 Opera 42
Dec, 2016 Apr, 2017 Mar, 2017 Sep, 2017 Dec, 2016

W3schools Pathfinder

Track your progress - it's free!

W3Schools.com (2024)
Top Articles
Colonial Penn Whole Life Insurance Review
8 Facts About the Kiddie Tax - The Little CPA
Terrorist Usually Avoid Tourist Locations
Myexperience Login Northwell
Archived Obituaries
Craigslist Parsippany Nj Rooms For Rent
Dark Souls 2 Soft Cap
2021 Tesla Model 3 Standard Range Pl electric for sale - Portland, OR - craigslist
Ncaaf Reference
Garrick Joker'' Hastings Sentenced
What Does Dwb Mean In Instagram
2135 Royalton Road Columbia Station Oh 44028
Morocco Forum Tripadvisor
Nwi Arrests Lake County
Payment and Ticket Options | Greyhound
Les Rainwater Auto Sales
Everything We Know About Gladiator 2
Free Online Games on CrazyGames | Play Now!
Craigslist Pet Phoenix
Finalize Teams Yahoo Fantasy Football
Graphic Look Inside Jeffrey Dahmer
Cincinnati Adult Search
Talkstreamlive
Chime Ssi Payment 2023
Https E22 Ultipro Com Login Aspx
Klsports Complex Belmont Photos
Dr. Nicole Arcy Dvm Married To Husband
Ou Football Brainiacs
Cinema | Düsseldorfer Filmkunstkinos
Housing Intranet Unt
The value of R in SI units is _____?
Chicago Pd Rotten Tomatoes
Workboy Kennel
M3Gan Showtimes Near Cinemark North Hills And Xd
Usf Football Wiki
USB C 3HDMI Dock UCN3278 (12 in 1)
Oxford Alabama Craigslist
Hingham Police Scanner Wicked Local
Why I’m Joining Flipboard
Dispensaries Open On Christmas 2022
Postgraduate | Student Recruitment
Vindy.com Obituaries
Portal Pacjenta LUX MED
Ferhnvi
Zom 100 Mbti
Bonecrusher Upgrade Rs3
El Patron Menu Bardstown Ky
Greg Steube Height
Booked On The Bayou Houma 2023
Latest Posts
Article information

Author: Ray Christiansen

Last Updated:

Views: 6049

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.