✨ Why Apps Crash (And How Smart Developers Keep Them Running)
😠 The classic: “Something went wrong…”
You tap a button in your favorite app. You wait. Suddenly... nothing. Or worse, the app freezes, and you get that lovely error message: “Oops! Something went wrong.”
Why does this happen? And more importantly—how can developers prevent it?
🌍 Apps talk to other computers (a lot)
Most modern apps don’t work alone. They constantly request data from servers. This is called an API request.
Imagine an app asking for:
- Temperature
- Wind speed
- Air quality
If just one server is down, everything fails. You get nothing.
🔥 The old way: All or nothing
Promise.all([
fetch('/temperature'),
fetch('/wind'),
fetch('/airquality')
])
.then(handleResults)
.catch(showErrorToUser);
If any one of these fetches fails, the entire thing fails. The user sees an error, even if some data was available.
✅ The smarter way: Take what works
Promise.allSettled([
fetch('/temperature'),
fetch('/wind'),
fetch('/airquality')
])
.then(results => {
results.forEach(result => {
if (result.status === 'fulfilled') {
console.log('✅ Success:', result.value);
} else {
console.warn('❌ Failed:', result.reason);
}
});
});
This lets your app continue to show what did work, instead of crashing entirely.
🧪 Real-world example: Trip planner app
Your travel app loads:
- 🚆 Next train to Brussels
- ✈️ Cheapest flight to Paris
- 🚕 Local taxis near you
Flight API down?
Old logic: Total crash.
New logic: Train + taxi still load. You keep going.
👨💻 Why it matters
APIs fail. Networks glitch. Servers hiccup. But your app shouldn’t collapse because of it.
Promise.allSettled()
helps you:
- Keep the app responsive
- Show partial results
- Avoid frustrating the user
✅ TL;DR
The Problem | The Fix |
---|---|
One API fails → All fail | One API fails → Others still work |
User sees nothing | User sees what’s available |
Want more dev insights without the fluff? Stick around—next up: Why some apps are painfully slow, and how developers speed them up.
Commentaires
Enregistrer un commentaire