简体   繁体   English

在node.js 6中合并(合并)回调

[英]Coalescing (combining) callbacks in node.js 6

I'm working with an AWS lambda function that has a sort of 'map/reduce' feel to it. 我正在使用具有某种“映射/减少”感觉的AWS lambda函数。 But the 'map' part of it, that is the part that does multiple calls is async. 但是它的“映射”部分(即执行多个调用的部分)是异步的。

Using the Node 6 STD lib, is there a dynamic way to get all results returned to a shared spot. 使用Node 6 STD库,有一种动态的方式将所有结果返回到共享点。

What I've thought about so far: 到目前为止我一直在想:

  • await async is a great abstraction but isn't in node 6 to my knowledge, just node 8. await async是一个很好的抽象,但据我所知,它不在节点6中,而仅在节点8中。
  • array.reduce says it takes a callback but the structure of an http request seems not to qualify though I certainly may be wrong array.reduce说它需要一个回调,但是http请求的结构似乎不合格,尽管我当然可能错了
  • I've thought about a really suboptimal solution, where each callback puts into a shared queue or array. 我考虑过一个真正次优的解决方案,其中每个回调都放入共享队列或数组中。 And I have a loop after all the requests that checks the length of the array - I don't like this solution though 而且在所有检查数组长度的请求之后都有一个循环-虽然我不喜欢这种解决方案

Could you guys point me in the right direction or show me some code that would do this? 你们能指出我正确的方向还是给我看一些可以做到这一点的代码?

Bluebird is your friend. 蓝鸟是你的朋友。

To convert callback functions into promises you can use .promisify() or .fromCallback() . 要将回调函数转换为.promisify()可以使用.promisify().fromCallback()

To do map/reduce over an array of promises, you can use .map() and .reduce() . 要在一个promise数组上进行映射/归约,可以使用.map().reduce()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM