Continuation of JavaScript

Can someone explain to me the concept of continuation passing in javascript? I try to figure this out by reading this http://nathansjslessons.appspot.com/lesson?id=1090 and trying to solve this task, but I can't solve it.

What would be the right way to do this?

I tried this:

var bothC = function (fC, gC, success, failure) {
    var f_success, f_failure;
    f_success = function () {
        success();
    };
    f_failure = function () {
        var g_success, g_failure;
        g_success = function () {
            success();
        };
        g_failure = function () {
            failure();
        };
        gC(g_success, g_failure);
    };
    fC(f_success, f_failure);
};
+4
source share
2 answers

I tried this

No, you basically just copied redefined your function seqC.

What would be the right way to do this?

function bothC(fC, gC, success, failure) {
    fC(function() {
        gC(success, failure);
    }, function() {
        gC(failure, failure);
    });
}
+2
source

A practical approach to this exercise will be to determine the utility function as follows:

function pipe(f, g) {
    return function(success, failure) {
        f(function() {
            g(success, failure)
        }, failure)
    }
}

, , . bothC :

var bothC = function (fC, gC, hC, success, failure) {
    pipe(fC, gC)(success, failure);
};

:

var allC = function (funcList, success, failure) {
    funcList.reduce(function(x, f) { return pipe(x, f)})(success, failure)
};
+1

Source: https://habr.com/ru/post/1541320/


All Articles