secret
Last active

  • Download Gist
gistfile1.md
Markdown

Here we compose sequences and gates from asynquence to handle "nested" async tasks:

var elems = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
// thanks to @ljharb: https://gist.github.com/ljharb/553b269d040deebd73b6
var segments = elems.map(function(el){
    return function(done){
        // delegate initial processing to a "nested" sequence
        processElement(el)
        // pipe the output of the nested sequence back into
        // our sequence
        .pipe(done);
    };
});


ASQ()
// parallel gate of processing for all elements
.gate.apply(null,segments)
// now we can inspect once all element processing is done
.val(function(){
    console.log(arguments);
    // [ 7, 28, 21, 56, 35, 84, 49, 112, 63, 140 ]
})
// oops, something went wrong somewhere along the way
.or(function(failMsg){
    console.log(failMsg);
    // process.exit()
});


function processElement(el) {
    return ASQ(function(done){
        setTimeout(function(){
            // check if we need to fail element processing
            if (el > 10) done.fail("too big: " + el);
            else done(el);
        },1000);
    })
    // delegate even more processing to a "nested" sequence,
    // and implicitly pipe that nested sequence's output
    // back into our sequence
    .seq(moreElementProcessing);
}


function moreElementProcessing(el) {
    return ASQ(function(done){
        setTimeout(function(){
            // let's double only the evens
            if (el % 2 === 0) el *= 2;
            done(el);
        },1000);
    })
    // do even more processing (this time in parallel)
    .gate(
        function(done,el){
            setTimeout(function(){
                // we can fail even really deep, and it will propagate out
                if (el === 72) done.fail("I don't like this number: " + el);
                else done(el * 3);
            },100);
        },
        function(done,el){
            setTimeout(function(){
                done(el * 4);
            },50);
        }
    )
    // re-combine the separately processed (gate) values back into
    // one value for each element
    .val(function(el_1,el_2){
        return el_1 + el_2;
    });
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.