Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

前端之编程训练——Compose #23

Open
jyzwf opened this issue Sep 20, 2017 · 0 comments
Open

前端之编程训练——Compose #23

jyzwf opened this issue Sep 20, 2017 · 0 comments

Comments

@jyzwf
Copy link
Owner

jyzwf commented Sep 20, 2017

var arr = [func1, func2, func3];
function func1(ctx, next) {
    ctx.index++
    next();
}
function func2(ctx, next) {
    setTimeout(function () {
        ctx.index++;
        next();
    });
}
function func3(ctx, next) {
    console.log(ctx.index);
}

const compose = arr => arr.reverse().reduce((a, b) => obj => b(obj, () => a(obj)), () => { })
compose(arr)({ index: 0 }); // 输出:2

/*
function compose(arr) {
    return arr.reverse().reduce((a, b) => {
        return function (obj) {
            b(obj, function () {
                a(obj)
            })
        }
    }, () => { })
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant