Thuta Learning
ရှာဖွေရန်
AdvancedProgrammingbeginner

Callbacks

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Callback သည် function တစ်ခုကို another function ရဲ့ argument အဖြစ် pass လုပ်ပြီး နောက်မှ execute လုပ်ခြင်းဖြစ်သည်။ 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