AdiA
Reactiflux4y ago
7 replies
Adi

✅ – Adi – 08-24 Jun 22

I am making async calls inside forEach but if some calls in foreach fail then it is not running the forEach block on remaining elements. I have already wrapped forEach inside try/catch block but did not work. I used try/catch inside forEach and then it worked. code:
 subs.forEach(async subscription => {
                try {
                    const endpoint = subscription.endpoint;
                    const id = endpoint.substr((endpoint.length - 8), endpoint.length);
                    const result = await webpush.sendNotification(subscription, notificationPayload, options);
                    console.log(`Endpoint ID: ${id}`);
                    console.log(`Result: ${result.statusCode}`);
                } catch (error) {
                    console.error("send notification error:", error)
                }
            });
By default forEach should not return early if any async call fails is that correct? then why in above case I have to use try/catch inside forEach??
Was this page helpful?