复杂的用例Node.js的异步模块 [英] Complicated use case for Node.js Async Module

查看:106
本文介绍了复杂的用例Node.js的异步模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Node.js的作为我的后端像DB查询/ API调用,等我读到的Node.js的异步并决定给它一个尝试。它一直致力于为简单的使用情况下,我想在并联或串联的一些任务,但不知何故,我在要求降落在我需要的异步的串联/并联/瀑布技术的最佳组合。

使用案例:

我有以下有回调实现的功能:

 函数A(输入,回调){
...
回调(ERR,结果);
}
函数B(输入1,输入2,回调){
...
回调(ERR,结果);
}
函数C(输入,回调){
...
回调(ERR,结果);
}
函数D(输入,回调){
...
回调(ERR,结果);
}
函数E(输入,回调){
...
回调(ERR,结果);
}

和他们的执行顺序应该是这样的:

A - > [B - > D]。结果
A - > [C - > E]


  • B,D,C,E的执行A.后

  • 后B执行ð

  • E被执行后,ç

  • B是一个后执行

  • c是一个后执行


  • B,D和C,E是不依赖于彼此,因此它们可以被执行
    在A已经执行完毕后,平行中。


  • D是依赖于B和应后B被执行


  • E是依赖于C和应该℃之后执行。

另外,我需要从每个功能的数据传给有关的功能。所以,应该paasing结果B和C同样B级传递给D和C传递到E

我怎样才能有效地利用异步模块执行它们?

我想,我需要这样的东西,但不能确定,但​​:

  async.waterfall(
  一个: ...,
  async.parallel(
      async.waterfall(
          B,
          ð
      )
      async.waterfall(
          C,
          Ë
      )
   )


解决方案

请尝试使用汽车方法。


  

确定为运行中的任务的功能的基础上,他们的要求的最佳顺序。每个功能可以任选取决于其他功能被首先完成,并且每个功能只要其要求已经满足运行


  
  

功能也收到一封含有迄今已完成的功能结果的对象。


例如:

  async.auto({
  答:函数(回调){
    回调(NULL,'数据');
  },
  B:'A',函数(回调,结果)
    //做一些results.A;
    回调(NULL,'B数据');
  }],
  C:['A',函数(回调,结果){
    //做一些results.A;
    回调(NULL,C数据');
  }],
  D:['A','B',函数(回调,结果){
    //做results.A和results.B的东西;
    回调(NULL,'D数据');
  }],
  E:['A','C',函数(回调,结果)
    //做results.A和results.C的东西;
    回调(NULL,'E数据');
  }]
},功能(错了,结果){
  的console.log('ERR =',ERR);
});

I have started using Node.js as my backend for performing different operations like DB queries/ API calls, etc. I was reading about Node.js Async and decided to give it a try. It has been working for simple use cases where I want some tasks in parallel or series but somehow I have landed in a requirement where I need an optimal combination of series/parallel/waterfall techniques of Async.

Use Case:

I have following functions implemented with callbacks:

function A(input, callback) {
...
callback(err,result);
}


function B(input1, input2, callback) {
...
callback(err,result);
}


function C(input, callback) {
...
callback(err,result);
}


function D(input, callback) {
...
callback(err,result);
}


function E(input, callback) {
...
callback(err,result);
}

And their order of execution should be like:

A -> [B -> D]
A -> [C -> E]

  • B,D,C,E are executed after A.
  • D is executed after B
  • E is executed after C
  • B is executed after A
  • C is executed after A

  • B,D and C,E are not dependent on one another, so they can be executed in paralled after A has completed execution.

  • D is dependent on B and should be executed after B

  • E is dependent on C and should be executed after C.

Also, I need to pass data from each of the functions to their dependent functions. So, A should be paasing result to B and C. Similarly B passing to D and C passing to E.

How can I execute them efficiently using Async module ?

I think I need something like but not sure though:

async.waterfall(    
  A: ...,    
  async.parallel(
      async.waterfall (
          B,
          D
      ),
      async.waterfall (
          C,
          E
      )
   )
) 

解决方案

Try using the auto method.

Determines the best order for running the functions in tasks, based on their requirements. Each function can optionally depend on other functions being completed first, and each function is run as soon as its requirements are satisfied.

Functions also receive an object containing the results of functions which have completed so far.

For example:

async.auto({
  A: function(callback){
    callback(null, 'A data');
  },
  B: ['A', function(callback, results)
    // do something with results.A;
    callback(null, 'B data');
  }],
  C: ['A', function(callback, results){
    // do something with results.A;
    callback(null, 'C data');
  }],
  D: ['A', 'B', function(callback, results){
    // do something with results.A and results.B;
    callback(null, 'D data');
  }],
  E: ['A', 'C', function(callback, results)
    // do something with results.A and results.C;
    callback(null, 'E data');
  }]
}, function(err, results) {
  console.log('err = ', err);
});

这篇关于复杂的用例Node.js的异步模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆