带有 Promise 的 async.mapLimit [英] async.mapLimit with Promise

查看:26
本文介绍了带有 Promise 的 async.mapLimit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用异步模块 (v3),尤其是 async.mapLimit 方法来提交有限数量的并行异步请求.这适用于以下(简化的)示例代码中的回调:

I'm trying to use the async module (v3) and especially the async.mapLimit method to submit a limited number of parallel asnychronous requests. This works well with callbacks in the following (simplified) sample code:

async = require('async');
async.mapLimit(['1','2','3','4','5'], 3, function(num, callback){
setTimeout(function(){
    num = num * 2,
    console.log(num);
    callback(null, num);
}, 
4000);  
},function(err, results){
    console.log(results);
});

结果我得到了单个值,最后得到了包含结果"中所有值的数组:

As result I get the single values and finally the array with all values in 'results':

[2,4,6,8,10]

现在我正在努力使用此方法的基于 Promise 的版本.文档 说如果我不提供打回来.如何将此基于回调的代码更改为使用 Promises?

Now what I'm struggling with is to use the Promise based version of this method. The documentation says it returns a Promise if I don't supply a callback. How can I change this callback-based code into using Promises?

我试过例如这个,但它只显示第一组请求(这是在评论中的第一个建议之后更新的):

I tried e.g. this, but it only shows the first set of requests (this is updated after the first suggestions in the comments):

let numPromise = async.mapLimit(['1','2','3','4','5'], 3, function(num, callback){
    setTimeout(function(){
        num = num * 2,
        console.log(num);
        callback(null, num);
    }, 
    4000); 
});
Promise.all([numPromise]) //always calls .then immediately, without result
.then((result) => console.log("success:" + result))
.catch(() => console.log("no success"));

我确实正确地取回了所有单个值,但是 '.then' 会立即执行并给我一个空的 'result' 变量的 'Success'.

I do get back all single values correctly, but the '.then' executes immediately and gives me 'Success' with an empty 'result' variable.

注意:我看过这个帖子 ES6 Promise 替换异步.eachLimit/async.mapLimit 当然,但我认为这不能回答我的问题.

Note: I have seen this thread ES6 Promise replacement of async.eachLimit / async.mapLimit of course, but I don't think that answers my question.

PS:我已经找到了解决这个问题的其他方法,但我对如何正确使用这个模块非常感兴趣(使用简短、干净的代码).

PS: I have found other solutions to this problem already, but I am really interested in how to use this module properly (with short, clean code).

推荐答案

这可能是更好的答案,尽管另一种选择似乎也不错:

Here's the probably better answer, though the other alternative seemed fine as well:

const async = require('async');
const delay = require('util').promisify(setTimeout);
const numPromise = async.mapLimit(['1','2','3','4','5'], 3, async num => delay(200).then(() => num*2))
// or const numPromise = async.mapLimit(['1','2','3','4','5'], 3, async num => {
//    await delay(200);
//    return num*2;
// })
numPromise.then(console.log)
// or numPromise.then((results) => console.log(results))

另请参阅 GitHub 上的此问题线程作为参考:GitHub:async.mapLimit 不返回 Promise

Please also see this issue thread on GitHub as reference: GitHub: async.mapLimit does not return a Promise

我相信我的代码的问题出在 iteratee 函数中 - 要么最初它不是异步的,要么使用回调"而不是我假设的返回"(或者可能两者兼而有之;-)).

I believe the issue with my code was in the iteratee function - either initially it wasn't asynchronous or using 'callback' instead of 'return' I assume (or maybe both ;-) ).

这篇关于带有 Promise 的 async.mapLimit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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