Asynchronous JavaScript allows tasks to run in the background, so the web page remains responsive.
A callback is a function passed into another function as an argument, which is executed once the task is completed:
function fetchData(callback) {
setTimeout(function() {
console.log("Data fetched");
callback();
}, 2000);
}
function displayData() {
console.log("Displaying data");
}
fetchData(displayData);
Create a function that simulates a delay and then calls another function to display a message after 3 seconds.
setTimeout
do?Answers: A function passed as an argument; delays execution of code; keeps the webpage responsive.