Skip to content

promise 的错误不需要对 Hermes 引擎特殊支持 #103

@littledu

Description

@littledu
export type PromiseUnCatchHandler = (id: string, error: Error) => void;

const noop: PromiseUnCatchHandler = () => { };

/**
 * 
 * @param {Function} customHandler
 * @param {Boolean} allowedInDevMode
 * @returns 
 */
export const setPromiseUnCatchHandler = (customHandler: PromiseUnCatchHandler = noop, allowedInDevMode = false) => {
    if (typeof customHandler !== 'function' || typeof allowedInDevMode !== 'boolean') {
        console.log('setPromiseUnCatchHandler is called with wrong argument types.. first argument should be callback function and second argument is optional should be a boolean');
        console.log('Not setting the JS handler .. please fix setPromiseUnCatchHandler call');
        return;
    }
    // @ts-ignore
    const allowed = allowedInDevMode ? true : !__DEV__;
    if (allowed) {
        // @ts-ignore
        require('promise/setimmediate/rejection-tracking').enable({
            allRejections: true,
            onUnhandled: customHandler,
            onHandled: (id: string) => {
                const warning =
                    `Promise Rejection Handled (id: ${id})\n` +
                    'This means you can ignore any previous messages of the form ' +
                    `"Possible Unhandled Promise Rejection (id: ${id}):"`;
                console.warn(warning);
            },
        });
    } else {
        console.log('Skipping setPromiseUnCatchHandler: Reason: In DEV mode and allowedInDevMode = false');
    }
};

看了如上的实现,跟我看了一些其他人的实现,如果是 Hermes 的话,则是:

global.HermesInternal?.enablePromiseRejectionTracker?.(
       cusotomtRejectionTrackingOptions,
);

请问,实际上是rejection-tracking就可以了么

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions