0%

解决promise调试的js代码

看到大佬写的一段解决promise调试的js代码,记录在此

class StoppablePromise {
    constructor(func) {
        if (typeof func === 'function')
            this._promise = new Promise(func);
        else
            this._promise = func;
    }

    catch_func_gen(catch_func) {
        return (data) => {
            if (data === 'break') {
                throw 'break'
            }
            catch_func(data)
        };
    }

    catch(func) {
        let promise = this._promise.catch(this.catch_func_gen(func));
        return new StoppablePromise(promise)
    }

    then(func) {
        let then = this._promise.then(func);
        return new StoppablePromise(then)
    }
}