Clarence-pan / shortcut-promise

A Promise which directly call onFullfilled or onRejected - shortcuted
1 stars 0 forks source link

我实现了一个更紧凑的 #1

Open dou4cc opened 7 years ago

dou4cc commented 7 years ago
const run = thunk => {
    const run = thunk => {
        try{
            thunk();
        }catch(error){
            setTimeout(() => {
                throw error;
            });
        }
    };
    try{
        return run(thunk());
    }catch(error){}
    Promise.resolve().then(() => run(() => thunk()()));
};
const [is_hell, hell] = (() => {
    const hells = new WeakSet;
    return [target => hells.has(target), () => {
        const hell = callback => {
            if(!callbacks) return run(() => () => callback(result));
            callbacks.push(callback);
        };
        let result;
        let callbacks = [];
        hells.add(hell);
        return [hell, result1 => {
            if(!callbacks) return;
            result = result1;
            const callbacks1 = callbacks;
            callbacks = null;
            callbacks1.forEach(hell);
        }];
    }];
})();
const tickline = (...ticks) => {
    switch(ticks.length){
    case 0:
        return target => {
            if(!is_hell(target)) return target;
            let target1;
            target(target => target1 = [tickline()(target)]);
            if(target1) return target1[0];
            const [, resolve] = [target1] = hell();
            target(target => {
                target = tickline()(target);
                if(is_hell(target)) return target(resolve);
                resolve(target);
            });
            return target1;
        };
    case 1:
        return target => {
            target = tickline()(target);
            if(is_hell(target)){
                const [hell0, resolve] = hell();
                target(target => resolve(tickline()((0, ticks[0])(target))));
                return tickline()(hell0);
            }
            try{
                return tickline()((0, ticks[0])(target));
            }catch(error){
                setTimeout(() => {
                    throw error;
                });
            }
            return hell()[0];
        };
    case 2: 
        return target => tickline(ticks[1])(tickline(ticks[0])(target));
    }
    return tickline(ticks.shift(), tickline(...ticks));
};
const gen2tick = gen => tickline(target => {
    target = gen.next(target);
    return target.done ? target.value : gen2tick(gen)(target.value);
});
const genfn2tick = genfn => function(){
    return gen2tick(genfn.apply(this, arguments))();
};
const same_list = (a, b) => a && a.length === b.length && a.every((a, i) => a === b[i] || Object.is(a, b[i]));
const cache = f => {
    let args;
    let result;
    const results = new WeakMap;
    return (...args1) => {
        if(same_list(args, args1)) return result;
        if(args1.length === 1){
            let result1 = results.get(args1[0]);
            if(result1) return result1[0];
            result1 = f(args1[0]);
            try{
                results.set(args1[0], [result1]);
                return result1;
            }catch(error){}
            result = result1;
        }else{
            result = f(...args1);
        }
        args = args1;
        return result;
    };
};
const prom2hell = cache(promise => {
    const [hell0, resolve] = hell();
    Promise.resolve(promise).then(
        result => resolve([null, result]),
        error => resolve([error]),
    );
    return hell0;
});
dou4cc commented 7 years ago

example:

(genfn2tick(function*(uri){
    const [error, response] = yield prom2hell(fetch(uri));
    if(error === null){
        const [error, text] = yield prom2hell(response.text());
        if(error === null) return text;
    }
}))(URL.createObjectURL(new Blob(["Hello World"])))(text => console.log(text));
dou4cc commented 7 years ago

修复了若干bug。

dou4cc commented 7 years ago

修复了若干bug。