Async.js Design Pattern

I am using async.js for my node.js. application I need help solving the following problem.

Let's say I have the following aysnc js series function.

async.waterfall([
    function getDataFromDB(request, response, callback){ ... },
    function someOperationOnDBData(dbData, response, callback){ ... },
    function renderSomeFlow(evaluatedData, response, callback){ ... }
]);

I have three functions called in the above order. I get data from getDataFromDBand going to someOperationOnDBDataand so on.

Suppose I need another operation between getDataFromDBand someOperationOnDBData, but still pass the DBData forward. For instance:

async.waterfall([
    function getDataFromDB(request, response, callback){ ... },
    function extraOperation(dbData, response, callback) {...}
    function someOperationOnDBData(dbData, extraOperationData, response, callback){ ... },
    function renderSomeFlow(evaluatedData, response, callback){ ... }
]);

Here, adding one step in the middle changes the definitions of the functions, and I also need to pass dbDatain extraOperationto just forward it to someOperationOnDBData. In addition, if I call another module in the middle, it may not be possible to change its parameter to send some data.

async.js ? , , . ?

+4
2

waterfall, - :). , , , auto, , .

auto , , . :

async.auto({
    db: function getDataFromDB(callback){ ... },
    extra: ['db', function extraOperation(results, callback) {...}],
    some: ['db', function someOperationOnDBData(results, callback){ ... }],
    render: ['some', 'db', function renderSomeFlow(results, callback){ ... }]
});
+4

, currying - :

const someOperationOnDBData = extraOperationData => (dbData, response, callback) => {
  // do your function work
};

async.waterfall([
  function getDataFromDB(request, response, callback){ ... },
  function extraOperation(dbData, response, callback) {...}
  someOperationOnDBData(extraOperationData)(dbData, response, callback),
  function renderSomeFlow(evaluatedData, response, callback){ ... }
]);

, javascript- Medium.com

0

All Articles