| کانال توسعه‌دهندگان وب |: post #100 — TG.ME

در کد زیر سعی شده در یک route مربوط به Express داده‌ها از یک API خارجی گرفته شده و به صورت JSON برگردانده شود، اما یک اشتباه/Anti Pattern جدی در استفاده از async/await و مدیریت error وجود دارد که می‌تواند باعث رفتارهای غیرقابل پیش‌بینی روی سرور شود.

با دقت به این کد نگاه کنید:

app.get('/posts', async (req, res) => {
try {
const response = fetch('https://example.com/api/posts');

response
.then(r => r.json())
.then(data => {
res.json({
success: true,
posts: data
});
})
.catch(err => {
console.error('Fetch error:', err);
res.status(500).json({ message: 'Internal error' });
});

} catch (err) {
console.error('Route error:', err);
res.status(500).json({ message: 'Route failed' });
}
});


چه چیزی باید در این کد اصلاح شود تا استفاده از async/await و مدیریت خطا به شکل درست و قابل پیش‌بینی انجام شود؟

🔖 #Web #وب #Frontend #Backend

👤 Developix

💎 Channel: @DevelopixWeb
May 21, 2026 776 1 1