Reputation: 14309
function sendPushNotification(subscription, urlEncodedData){
try {
webpush.sendNotification(subscription, urlEncodedData);
} catch (err){
console.log('err');
console.log(err);
}
}
This doesn't catch the error, it is still considered an 'unhandledRejection'. Why?
Upvotes: 2
Views: 41
Reputation: 211700
If you're calling an async
function, or a function that returns a Promise
then you need to sequence things properly:
async function sendPushNotification(subscription, urlEncodedData){
try {
await webpush.sendNotification(subscription, urlEncodedData);
} catch (err){
console.log('err');
console.log(err);
}
}
Here await
will capture any response. An error condition that will manifest as an exception.
Note that this makes sendPushNotification()
return a Promise
so you will have to treat it as asynchronous. This means the caller needs to await
as well, and that may have impacts up the call chain depending on your sequencing requirements.
Upvotes: 2