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
Are you financially better off single? - Times Money Mentor
The Libeskind Building | Jewish Museum Berlin
What Is Single Sign-on (SSO)? Meaning and How It Works? | Fortinet
Gomoviesmalayalam
Gamevault Agent
Phcs Medishare Provider Portal
Lifebridge Healthstream
Xrarse
Evita Role Wsj Crossword Clue
Truist Drive Through Hours
The Wicked Lady | Rotten Tomatoes
Cranberry sauce, canned, sweetened, 1 slice (1/2" thick, approx 8 slices per can) - Health Encyclopedia
Oxford House Peoria Il
Darksteel Plate Deepwoken
Who called you from 6466062860 (+16466062860) ?
National Office Liquidators Llc
Wilmot Science Training Program for Deaf High School Students Expands Across the U.S.
National Weather Service Denver Co Forecast
Bnsf.com/Workforce Hub
U Break It Near Me
Shopmonsterus Reviews
zom 100 mangadex - WebNovel
Self-Service ATMs: Accessibility, Limits, & Features
Great Clips Grandview Station Marion Reviews
R. Kelly Net Worth 2024: The King Of R&B's Rise And Fall
Jeff Nippard Push Pull Program Pdf
Telegram Voyeur
Pioneer Library Overdrive
What is Software Defined Networking (SDN)? - GeeksforGeeks
Ff14 Laws Order
Mbi Auto Discount Code
Appleton Post Crescent Today's Obituaries
Sitting Human Silhouette Demonologist
Craigslist Albany Ny Garage Sales
Terrier Hockey Blog
American Bully Xxl Black Panther
The Blackening Showtimes Near Regal Edwards Santa Maria & Rpx
Dr. John Mathews Jr., MD – Fairfax, VA | Internal Medicine on Doximity
Admissions - New York Conservatory for Dramatic Arts
How To Paint Dinos In Ark
Craigs List Palm Springs
R/Moissanite
Complete List of Orange County Cities + Map (2024) — Orange County Insiders | Tips for locals & visitors
Aurora Il Back Pages
Ssc South Carolina
Breaking down the Stafford trade
Dyi Urban Dictionary
El Patron Menu Bardstown Ky
Uno Grade Scale
Wieting Funeral Home '' Obituaries
Scholar Dollar Nmsu
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.