जैसा कि नाम से पता चलता है कि async फंक्शन डिक्लेरेशन एक एसिंक्रोनस फंक्शन को परिभाषित करता है। यह फ़ंक्शन एक AsyncFunction ऑब्जेक्ट देता है।
सिंटैक्स
यहां सिंटैक्स है -
async function functionname([param[, param[, ... param]]]) {
statements to be executed
} उदाहरण
आइए एक उदाहरण देखते हैं, जो 5 सेकंड के बाद परिणाम प्रिंट करता है -
<html>
<body>
<script>
function displayFunction(num) {
return new Promise(resolve => {
setTimeout(() => {
resolve(num);
}, 5000);
});
}
async function add2(num) {
const x = displayFunction(7);
const y = displayFunction(5);
return num * await x * await y;
}
add2(15).then(result => {
document.write("Multiplication Result (after 5 seconds): "+result);
});
</script>
</body>
</html>