my.onUnhandledRejection

更新时间:
复制 MD 格式

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 reject(), typically an Error object.

promise

Promise

The rejected Promise object.

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);
 }
})
Note
  • If the callback function of my.onUnhandledRejection triggers another unhandledrejection event, it may cause an infinite loop. Avoid triggering unhandled Promise rejections inside the callback.

  • All unhandledRejection events are captured by this listener, but only Error types trigger an alarm in the Mini Program console.