snippetjavascriptTip
What is a callback function?
Viewed 0 times
javascriptwhatcallbackfunction
Problem
A callback function is a function passed as an argument to another function, which is then invoked inside the outer function. Callback functions are often executed once an event has occurred or a task has completed.
A synchronous callback is a callback function that is executed immediately. The function passed as the first argument to
An asynchronous callback is a callback function that is used to execute code after an asynchronous operation has completed. The function executed inside
A synchronous callback is a callback function that is executed immediately. The function passed as the first argument to
Array.prototype.map() is a great example of a synchronous callback:An asynchronous callback is a callback function that is used to execute code after an asynchronous operation has completed. The function executed inside
Promise.prototype.then() is a great example of an asynchronous callback:Solution
const nums = [1, 2, 3];
const printDoublePlusOne = n => console.log(2 * n + 1);
nums.map(printDoublePlusOne); // LOGS: 3, 5, 7An asynchronous callback is a callback function that is used to execute code after an asynchronous operation has completed. The function executed inside
Promise.prototype.then() is a great example of an asynchronous callback:Code Snippets
const nums = [1, 2, 3];
const printDoublePlusOne = n => console.log(2 * n + 1);
nums.map(printDoublePlusOne); // LOGS: 3, 5, 7const nums = fetch('https://api.nums.org'); // Suppose the response is [1, 2, 3]
const printDoublePlusOne = n => console.log(2 * n + 1);
nums.then(printDoublePlusOne); // LOGS: 3, 5, 7Context
From 30-seconds-of-code: callbacks
Revisions (0)
No revisions yet.