forEach Doesn't Wait for Your await: The Bug That Makes APIs Reply Before the Work Is Done

javascript dev.to

Your endpoint sends 50 emails, marks 50 orders as shipped, and returns { success: true }. In your tests, it works. In production, the response comes back in 8 milliseconds, half the orders are still pending, and one failed email crashed the whole Node process with an error nobody can trace to a request. The culprit is usually one innocent line. The Code That Looks Fine app.post('/api/ship', async (req, res) => { const orders = await Order.find({ status: 'paid' }); orders.fo

Read Full Tutorial open_in_new
arrow_back Back to Tutorials