A function that is passed as a parameter to another function.
A function that receives a parameter can execute the callback function immediately(synchronously) or later(asynchronously) as needed.
function B() {
console.log('called at the back!');
}
function A(callback) {
callback(); // callback === B
}
callback in action: function to run repeatedly(iterator)
[1, 2, 3].map(function(element, index) {
return element * element;
});
callback in action: function according to the event(event handler)
document.querySelector('#btn').addEventListener('click', function(e) {
console.log('button clicked');
});
Don't get confused!
function handleClick() {
console.log('button clicked');
};
Correct Answers:
document.querySelector('#btn').onclick = handleClick;
document.querySelector('#btn').onclick = function () {
handleClick()
}
document.querySelector('#btn').onclick = handleClick.bind();
Wrong Answers:
document.querySelector('#btn').onclick = handleClick();
** It's not about linking function execution. Link the function itself.
Phone Call
1. You have to stop what you're doing and receive a new call(blocking).
Text message
1. After checking the text, you can reply later (non-blocking).
Phone Call
2. The result of the request happens at the same time (synchronous).
Text Message
2. The results fo the requests do not occur at the same time. (asynchronous)
Synchronous
What if coffee orders work synchronously?
1. Customer 1 orders an Americano.
2. The staff brews the Americano.
3. The staff delivers an Americano to Customer 1.
4. Customer 2 orders an cafe latte.
5. The staff starts making the cafe latte.
6. The employee delivers a cafe latte to Customer 2.
** Guest 2 can't even order and has to stay in the queue until Guest 1 gets their Americano delivered.
Asynchronous
Asynchronous function passing pattern 1: callback pattern
let request = 'cafe latte';
orderCoffeeAsync(request, function(response) {
// response --> coffee order result
drink(response);
});
Asynchronous function passing pattern 2: event registration pattern
let request = 'cafe latte';
orderCoffeeAsyn(request).onready = function(response) {
// response --> coffee order result
drink(response);
});
DOM Element event handlers
- Mouse and keyboard input(click, keydown, etc.)
- Page loading (DOMcontentLoaded etc.
Timer
- Timer API (setTimeout, etc.)
- Animation API (requestAnimationFrame)
Requesting and Responsing resources from/to the server.
- fetch API
- AJAX (XHR)
Synchronous Coffee Order
function waitSync(ms) {
var start = Date.now();
var now = start;
while (now - start < ms) {
now = Date.now();
}
}
function drink(person, coffee) {
console.log(person + 'is drinking' + coffee);
}
function orderCoffeeSync(coffee) {
console.log(coffee + 'is ready.');
waitSync(4000);
return coffee;
}
let customers = [{
name: 'Steve',
request: 'cafe latte'
}, {
name: 'John',
request: 'Americano'
}];
// call synchronously
customers.forEach(function(customer) {
let coffee = orderCoffeeSync(customer.request);
drink(customer.name, coffee);
});
function waitAsync(callback, ms) {
setTimeout(callback, ms);
}
function drink(person, coffee) {
console.log(person + 'drinks' + coffee);
}
let customers = [{
name: 'Steve',
request: 'cafe latte'
}, {
name: 'John',
request: 'americano'
}];
function orderCoffeeAsync(menu, callback) [
console.log(menu + 'is ready');
waitAsync(function() {
callback(menu);
}, 4000);
}
// call asynchronously
customers. forEach(function(customer) {
orderCoffeeAsync(customer.request, function(coffee) {
drink (customer.name, coffee);
});
});