एसिंक फ़ंक्शन घोषणा, जैसा कि नाम से पता चलता है, एक एसिंक्रोनस फ़ंक्शन को परिभाषित करती है। यह फ़ंक्शन एक AsyncFunction ऑब्जेक्ट लौटाता है।
सिंटैक्स
यहाँ सिंटैक्स है:
async function functionname([param[, param[, ... param]]]) {
निष्पादित किए जाने वाले कथन
}
उदाहरण
आइए एक उदाहरण देखें, जो 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>