Question 20
Question
Answer
function fetchData(url, callback) {
// Simulate a network request with setTimeout
setTimeout(() => {
if (Math.random() < 0.8) { // 80% chance of success
callback(null, "Data fetched successfully!");
} else {
callback("Network error occurred");
}
}, 1000);
}
fetchData('https://example.com/api', (error, data) => {
if (error) {
console.error("Error:", error);
} else {
console.log("Data:", data);
}
});Last updated