13.JavaScript Async

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

JavaScript Async

Callback function:

★ A callback function can be defined as a function passed into


another function as a parameter.
★ Don't relate the callback with the keyword, as the callback is just
a name of an argument that is passed to a function.
★ In other words, we can say that a function passed to another
function as an argument is referred to as a callback function.
★ The callback function runs after the completion of the outer
function.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function getdata(x,y,callback){
console.log(x+y+"<br>");
callback();
}
function showdata(){
document.write("Hello world");
}
getdata(10,20,showdata);
</script>
</body>
</html>

Another Example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function getdata(name){
alert("Hello "+name);
}
function showdata(callback){
let name = prompt("Welcome :) What is your name?");
callback(name);
}
showdata(getdata);
</script>
</body>
</html>

You might also like