function fetchData(url, callback) {
// Simulate an asynchronous operation, like fetching data from an API
setTimeout(() => {
const data = 'Sample data from ' + url;
callback(data); // Call the callback function with the data
}, 1000); // Simulate a 1 second delay
}
// Define a function that will act as the callback
function processData(data) {
console.log('Received:', data);
}
// Pass processData as a callback function to fetchData
fetchData('https://api.example.com', processData);
function fetchData(url, callback) { // Simulate an asynchronous operation, like fetching data from an API setTimeout(() => { const data = 'Sample data from ' + url; callback(data); // Call the callback function with the data }, 1000); // Simulate a 1 second delay }
// Define a function that will act as the callback function processData(data) { console.log('Received:', data); }
// Pass
processData
as a callback function tofetchData
fetchData('https://api.example.com', processData);