jadjoubran / codetogo.io

🚀 JavaScript code to go - Find updated snippets for common JavaScript use cases
https://codetogo.io
MIT License
233 stars 30 forks source link

Use Case Suggestion: function as argument #351

Open lastarcher opened 8 months ago

lastarcher commented 8 months ago

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);