Listens for unhandled Promise rejection events in mini programs.
Description
my.onUnhandledRejection
Listens for unhandled Promise rejection events.
Input parameter
Function listener
The callback function for unhandled Promise rejection events.
Parameter
Object res
|
Attribute |
Type |
Description |
|
reason |
any |
The rejection reason. The value passed to |
|
promise |
Promise |
The rejected Note
Android supports this field starting with Base Library 2.9.7. |
Sample code
my.onUnhandledRejection(Function listener)
Page({
onLoad() {
my.onUnhandledRejection(this.unhandledRejectionHandler);
},
unhandledRejectionHandler(res) {
console.log('onUnhandledRejection reason', res.reason);
console.log('onUnhandledRejection promise', res.promise);
}
})
-
If the callback function of
my.onUnhandledRejectiontriggers anotherunhandledrejectionevent, it may cause an infinite loop. Avoid triggering unhandledPromiserejections inside the callback. -
All
unhandledRejectionevents are captured by this listener, but onlyErrortypes trigger an alarm in the Mini Program console.