🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🪟 Windows TippsSetting up live captions stuck in Windows 11(14.09.2026 um 16:31 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsWindows 11's latest update broke my speakers, and I'm not alone(14.09.2026 um 18:54 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🪟 Windows TippsSetting up live captions stuck in Windows 11(14.09.2026 um 16:31 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsWindows 11's latest update broke my speakers, and I'm not alone(14.09.2026 um 18:54 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 2 Min Lesezeit
0

My React Journey: Day 12

↗ Quelle (dev.to)
🗣️ Stimme:

Practice with Callbacks andPromises



Today, I took on challenges to deepen my understanding of callbacks and promises—both foundational concepts for asynchronous programming in JavaScript. Here’s how it went:



Challenge 1: Callback - Event Registration Simulation



Scenario:

Simulated an event registration system where:




  • A user tries to register for an event.

  • The system checks if there are available slots.

  • Success or failure is communicated via callbacks.



Code Implementation




CODE
// Event Data
const event = {
name: "React Masterclass",
maxSlots: 5,
currentRegistration: 3,
};

// Registration Function
function registerForEvent(event, user, successCallback, errorCallback) {
console.log(`Registration of ${user} in progress...`);
setTimeout(() => {
if (event.currentRegistration < event.maxSlots) {
event.currentRegistration++;
successCallback(`Congratulations, ${user}. You have been registered for ${event.name}`);
} else {
errorCallback(`Sorry ${user}. The event space ${event.name} is fully booked`);
}
}, 2000); // Simulating 2-second delay
}

// Callbacks
function onSuccess(message) {
console.log("Success:", message);
}

function onError(error) {
console.log("Error:", error);
}

// Simulate Registration
registerForEvent(event, "Damilare", onSuccess, onError);







Output:




  • If slots are available:
    Success: Congratulations, Damilare. You have been registered for React
    Masterclass.

  • If slots are full:
    Error: Sorry Damilare. The event space React Masterclass is fully booked.



Reflection:

This challenge highlighted how callbacks handle asynchronous tasks, such as processing delays and managing outcomes.



Challenge 2: Promises - Delayed Welcome Message



Scenario:

Create a function that returns a welcome message after a specified delay using promises.



Code Implementation




CODE

// Promise Function
function delayedWelcomeMessage(message, delay) {
return new Promise((resolve, reject) => {
if (delay <= 0) {
reject("Delay must be greater than 0 milliseconds");
} else {
setTimeout(() => {
resolve(message);
}, delay);
}
});
}

// Valid Delay
delayedWelcomeMessage("Welcome to the world of promises", 3000)
.then((message) => console.log("SUCCESS:", message))
.catch((error) => console.error("ERROR:", error));

// Invalid Delay
delayedWelcomeMessage("This will fail.", 0)
.then((message) => console.log("SUCCESS:", message))
.catch((error) => console.error("ERROR:", error));






Output:




  • For a valid delay:

    After 3 seconds:

    SUCCESS: Welcome to the world of promises


  • For an invalid delay (e.g., 0):

    ERROR: Delay must be greater than 0 milliseconds




Reflection:

This exercise reinforced how promises improve readability and manage asynchronous flows better than callbacks, especially when dealing with multiple steps.



Takeaways:

Callbacks: Useful for managing simple async operations but can get messy with nesting (callback hell).

Promises: Provide a cleaner, more scalable approach to handling async tasks.

Combining these challenges with real-world scenarios (like event registration) made the concepts more relatable and fun to practice.



I am excited!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
The Gemini desktop app is now available for Windows
1 Quelle
Setting up live captions stuck in Windows 11
1 Quelle
Burn Out, Or Fade Away
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten My React Journey: Day 12

Thematisch verwandte Begriffe: React, Journey · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...