converting code from not CPS to CPS (CPS aka Continuation Passing Style aka Continuations)

Posted by Delirium tremens on Stack Overflow See other posts from Stack Overflow or by Delirium tremens
Published on 2010-03-12T17:03:32Z Indexed on 2010/03/12 18:47 UTC
Read the original article Hit count: 419

Filed under:
|
|

before:

function sc_startSiteCompare(){
    var visitinguri;
    var validateduri;
    var downloaduris;
    var compareuris;
    var tryinguri;
    sc_setstatus('started');
    visitinguri = sc_getvisitinguri();
    validateduri = sc_getvalidateduri(visitinguri);
    downloaduris = new Array();
    downloaduris = sc_generatedownloaduris(validateduri);
    compareuris = new Array();
    compareuris = sc_generatecompareuris(validateduri);
    tryinguri = 0;
    sc_finishSiteCompare(downloaduris, compareuris, tryinguri);
}
function sc_getvisitinguri() {
    var visitinguri;
    visitinguri = content.location.href;
    return visitinguri;
}

after (I'm trying):

function sc_startSiteCompare(){
    var visitinguri;
    sc_setstatus('started');
    visitinguri = sc_getvisitinguri(sc_startSiteComparec1);
}
function sc_startSiteComparec1 (visitinguri) {
    var validateduri;
    validateduri = sc_getvalidateduri(visitinguri, sc_startSiteComparec2);
}
function sc_startSiteComparec2 (visitinguri, c) {
    var downloaduris;
    downloaduris = sc_generatedownloaduris(validateduri, sc_startSiteComparec3);
}
function sc_startSiteComparec3 (validateduri, c) {
    var compareuris;
    compareuris = sc_generatecompareuris(downloaduris, validateduri, sc_startSiteComparec4);
}
function sc_startSiteComparec4 (downloaduris, compareuris, validateduri, c) {
    var tryinguri;
    tryinguri = 0;
    sc_finishSiteCompare(downloaduris, compareuris, tryinguri);
}
function sc_getvisitinguri(c) {
    var visitinguri;
    visitinguri = content.location.href;
    c(visitinguri);
}

What should the code above become?

I need CPS, because I have XMLHttpRequests when validating uris, then downloading pages, but I can't use return statements, because I use asynchronous calls. Is there an alternative to CPS?

Also, I'm having to pass lots of arguments to functions now. global in procedural code look like this / self in modular code. Any difference? Will I really have to convert from procedural to modular too?

It's looking like a lot of work ahead.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about cps