4

Can someone please explain to me the concept of continuation passing in javascript? I'm trying to understand it by reading this http://nathansjslessons.appspot.com/lesson?id=1090 and trying to solve the exercise given, but I can't seem to solve it.

What would be the correct way of doing it?

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);
};
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Radu
  • 524
  • 1
  • 6
  • 19
  • 4
    I've never heard the term "continuation" before. – gen_Eric May 20 '14 at 17:51
  • 4
    It is similar concept to promises. Instead of returning a value you accept a callback to call that is passed in as a parameter when the current method is done. – Mike Cheel May 20 '14 at 17:52
  • I've used JavaScript queues in which you pass the next function in the queue as an argument. I just never knew there was a term for it ;-) – gen_Eric May 20 '14 at 17:53
  • 3
    @RocketHazmat It's very common! Read more about it here https://en.wikipedia.org/wiki/Continuation-passing_style – Joe May 20 '14 at 18:25

2 Answers2

2

I tried this

No, you've basically just copiedreimplemented their seqC function.

What would be the correct way of doing it?

function bothC(fC, gC, success, failure) {
    fC(function() {
        gC(success, failure);
    }, function() {
        gC(failure, failure);
    });
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • yes, you are right, I just copied that in an attempt to understand the errors that it would give me and have a starting point. Can you explain step-by-step why this works? – Radu May 20 '14 at 19:14
  • You should've started with the concise "simplified" version of `seqC`, it's much easier to understand (its only benefit are explicit function names that can be used for explanations). I don't know really what to explain step-by-step, can you tell me at which point you are confused? – Bergi May 20 '14 at 19:43
  • Sure. Let me tell you what I am understanding so far. fC and gC are two functions passed as parameters and success and failure are continuations for each function. When I am calling fC with those two functions as paramaters, I don't understand what gC returns and what are those two identical lines of code( function() { gC(success, failure) } ) are doing. – Radu May 20 '14 at 20:27
  • 1
    `fC` and `gC` don't `return`, they call the passed continuations (that's what CP style is all about!). The two functions that are passed as parameters to `fC` are exactly those continuation callbacks. And notice that the two lines are not identical - if `fC` did execute the fail continuation, `gC` is passed the `failure` continuation both for its success and failure parameter. – Bergi May 20 '14 at 20:32
1

A practical approach to this exercise would be to define an utility function like this:

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

This higher-order function composes two given functions so that the second one will only run if the first succeeds. Now the implementation of bothC becomes trivial:

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

Multiple arguments are no problem too:

var allC = function (funcList, success, failure) {
    funcList.reduce(function(x, f) { return pipe(x, f)})(success, failure)
};
gog
  • 10,367
  • 2
  • 24
  • 38