Quick actions

cmd+k|ctrl+k

Navigation

Languages

Callback Function

Snippet info

Language

JavaScript

Visibility

public

Author

bookingnationsound

Created

2024-11-15T12:46:15.551079Z

Updated

2024-11-15T12:47:08.675538Z

// A function that simulates a long-running task using a timeout
function fetchData(callback) {
  console.log("Fetching data... Please wait.");

  // Simulate a network request using setTimeout
  setTimeout(() => {
    console.log("Data fetched successfully!");
    const data = { name: "Martins", age: 24 };

    // Call the callback function with the fetched data
    callback(data);
  }, 2000); // 2-second delay to simulate async operation
}

// A callback function to handle the fetched data
function displayData(data) {
  console.log("Displaying data:");
  console.log(`Name: ${data.name}`);
  console.log(`Age: ${data.age}`);
}

// Call the function with the callback
fetchData(displayData);
INFO