We may get the error reason or the error's stack if one exists to record in logs, show it on page/console and so on, but how can be used the promise reference passed to handler as second parameter?
process.on('unhandledRejection', (err, p) => {
console.log(err); // "some reason"
console.log(p); // Promise { <rejected> 'some reason' }
});
The promise reference is passed in so that the handler can distinguish which promise was rejected. The error itself doesn't help with that, in fact you can have multiple different promises rejected with the same error, and might get multiple unhandled rejections from them.
Now the promise object itself is not particularly useful, you can't do a lot with it except to attach handlers - and you already know that is was rejected and with which value. In some rare cases, the promise might be an instance of some promise subclass which carries extra data around, so an unhandled rejection could in theory use that; but this is unusual.
What's more relevant is that the promise object has an identity, which you can use to identify the rejection. This in particular is relevant for the rejectionhandled
event which will tell you that a rejected, unhandled promise now got a handler attached. You would use the promise as an identifier - e.g. as a key in a Map
- to correlate it to the previous unhandledrejection
.