Catch all unhandled javascript promise rejections

后端 未结 4 671
青春惊慌失措
青春惊慌失措 2020-11-27 05:01

I would like to catch all unhandled exceptions/rejections that take place within a javascript Promise. Is there a good method for catching them without adding a .catch

4条回答
  •  执笔经年
    2020-11-27 05:52

    The whole world is waiting for the unhandledrejection and rejectionhandled events. As of March 2016, Chrome is now the first to support it.

    Example:

    window.addEventListener('unhandledrejection', function(event) {
        console.error('Unhandled rejection (promise: ', event.promise, ', reason: ', event.reason, ').');
    });
    

    Specification: HTML Living Standard

    Mozilla Developer: onrejectionhandled, onunhandledrejection

    Chromium Issues: 495801, 393913

提交回复
热议问题