Thuta Learning
AdvancedProgrammingbeginner

Callbacks

Relax. We'll talk through this in plain words — no textbook voice.

Callback is a function passed as an argument to another function, to be executed later. It's widely used for asynchronous operations.

⚡ Common Uses:

• Event handlers

• setTimeout/setInterval

• Array methods (map, filter)

• AJAX requests

javascript
// Basic callback
function greet(name, callback) {
    console.log(`Hello, ${name}!`);
    callback();
}

greet("Aung Kyaw", function() {
    console.log("Callback executed!");
});

// Array methods with callbacks
const numbers = [1, 2, 3, 4, 5];

const doubled = numbers.map(function(num) {
    return num * 2;
});
console.log(`Doubled: ${doubled}`);

const evens = numbers.filter(function(num) {
    return num % 2 === 0;
});
console.log(`Evens: ${evens}`);

// Callback hell example (why promises were invented)
function step1(callback) {
    setTimeout(() => {
        console.log("Step 1 complete");
        callback();
    }, 100);
}

step1(() => {
    console.log("Step 2 complete");
});
You should see
Hello, Aung Kyaw! Callback executed! Doubled: 2,4,6,8,10 Evens: 2,4 Step 1 complete Step 2 complete
Callbacks | Thuta Learning