Asynquence

In notebook:
FrontEndMasters Real-Time Web with Node.js
Created at:
2015-10-13
Updated:
2015-10-13
Tags:
JavaScript libraries
Video Course on Real-Time HTML5 with Node.jstalking about nested callbacks: implicit trust, inverson of control, less flexibility

introducing his library asynquence 

​npm install asynquence asynquence-contrib​ the contrib has the additional plugins

​var ASQ = require("asynquence");
require("asynquence-contrib");​ <- don't need to attach to a variable, doesn't return anything (retursn ​null​)
how to express the previous example as a sequence of things:
  1. read a file and wait for it to finish
  2. call a timeout and wait for it to finish (database simulation)
  3. send the contents out
refactor the previous code Asynchronous File IO
  function delayMsg(done, contents){ // done is how asynquence works
    setTimeout(function(){
        done(contents);
    }, 1000)
}

function readFile(filename){
    var sq = ASQ();
    
    fs.readFile(filename, sq.errfcb());
    
    return sq;
    
}

function say(filename){
    return readFile(filename)
        .then(delayMsg);
}
in the code above, the interesting part:

function readFile(filename){
    var sq = ASQ();    
    fs.readFile(filename, sq.errfcb());    
    return sq;
}

the ​sq.errfcb()​ does all the ​if​ / ​else​ wrapping. It adapts a callback style to a flow control style (use promises style in an environment that is callback style)
errfcb means "error first callback"
  sq.then(function(done){
    fs.readFile(filename,)
});