JavaScript Async
JavaScript Async
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT
JavaScript Async
❮ Previous Next ❯
Async Syntax
The keyword async before a function makes the function return a promise:
Example
function myFunction() {
return Promise.resolve("Hello");
}
myFunction().then(
function(value) { /* code if successful */ },
function(error) { /* code if some error */ }
);
Example
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT
Or simpler, since you expect a normal value (a normal response, not an error):
Example
Try it Yourself »
Await Syntax
The await keyword can only be used inside an async function.
The await keyword makes the function pause the execution and wait for a resolved promise before it continues:
Example
Let's go slowly and learn how to use it.
Basic Syntax
myDisplay();
Try it Yourself »
We will not create them, but call one of them when the executor function is ready.
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT
async function myDisplay() {
let myPromise = new Promise(function(resolve) {
resolve("I love You !!");
});
document.getElementById("demo").innerHTML = await myPromise;
}
myDisplay();
Try it Yourself »
myDisplay();
Try it Yourself »
getFile();
Try it Yourself »
Browser Support
ECMAScript 2017 introduced the JavaScript keywords async and await .
The following table defines the first browser version with full support for both:
Tutorials Exercises Services Spaces Get Certified My W3Schools
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT
Chrome 55 Edge 15 Firefox 52 Safari 11 Opera 42
Dec, 2016 Apr, 2017 Mar, 2017 Sep, 2017 Dec, 2016
❮ Previous Next ❯
CONTACT US
Get Certified
HTML Certificate
CSS Certificate
JavaScript Certificate
Front End Certificate
SQL Certificate
Python Certificate
PHP Certificate
jQuery Certificate
Java Certificate
C++ Certificate
C# Certificate
XML Certificate
Tutorials Exercises Services Spaces Get Certified My W3Schools
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.