简体   繁体   English

如何使用Async库实现异步操作?

[英]How do I Promisify an Async operation, using the Async library?

GrabRedisDataByArray = Promise.promisify(function(data, callback) {
    var temp_results = [];

    async.each(data, function(result, done) {

        redis.hgetall('username:' + result, function(err, results) {

            temp_results.push(results);
            done();
        })

    }, function(err) {
        callback(temp_results)
    });

});

Except, it's returning 除了,它回来了

Unhandled rejection (<[{"server":"9300","user_id":"31","char...>, no stack trace )

--That object data is just some user information stored in the username: key. -对象数据只是存储在username:键中的一些用户信息。


And my method for data retrieval is: 我的数据检索方法是:

 GrabRedisDataByArray(data).then(function(data){
         console.log(data)                               
 });

I think my problem is with GrabRedisDataByArray and how it doesn't return anything in the main function scope, but only returns something through its second parameter? 我认为我的问题出在GrabRedisDataByArray ,它如何在主函数范围内不返回任何内容,而仅通过其第二个参数返回什么? If that makes sense? 那有道理吗? I'm just kind of lost here and trying to understand how I can return that async operation to the main function instead of only returning it through the second parameter callback (Which is I think my promises isn't working correctly) 我在这里有点迷失,试图了解如何将异步操作返回到主函数,而不是仅通过第二个参数回调返回(这是我认为我的诺言无法正常工作)

Edit: This is using the Bluebird library 编辑:这是使用蓝鸟库

Well, promisify expects node-style callbacks, so when you're calling callback(temp_results) you are passing an error parameter. 好吧, promisify需要节点样式的回调,因此,当您调用callback(temp_results)传递一个error参数。 It would have to be callback(null, temp_results) . 它必须是callback(null, temp_results)


That said, you should not use the async library at all when you're working with promises anyway. 就是说,无论如何在使用async时都不应该使用async库。 Just embrace promises. 只是信守承诺。 Or if you insist on using async , at least do it properly: 或者,如果您坚持使用async ,至少请正确执行以下操作:

var grabRedisDataByArray = Promise.promisify(function(data, callback) {
    async.map(data, function(result, done) {
        redis.hgetall('username:' + result, done);
    }, callback);
});

But when promisifying, you should always promisify on the lowest level - in your case redis . 但是,在进行承诺时,应该始终在最低级别进行承诺-在您的情况下为redis Then you don't have to mess around with async callbacks but can simply use Bluebird's Promise.map : 然后,您不必弄乱async回调,而只需使用Bluebird的Promise.map

Promise.promisifyAll(redis);
function grabRedisDataByArray(data) {
    return Promise.map(data, function(result) {
        return redis.asyncHgetall('username:' + result);
    });
}

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

相关问题 我可以仅通过使用异步来“实现”功能吗? - Can I “promisify” a function by just using async? 我怎么知道上一次异步操作何时完成? - How do I know when the last async operation will finish? 如何在Async库的“Each”函数中定义回调? - How do I define the callback in the Async library's “Each” function? readFileSync和使用async / await在readFile之上使用promisify之间的区别 - Difference between readFileSync and using promisify on top of readFile with async/await 承诺基础。 如何使异步节点代码合理化? - Promises basics. How to promisify async node code? 如何使用npm库进行异步集成测试? - How can I deliver async integration testing using npm library? 我使用Async的Library Parallel方法错了吗? 但是怎么样? - I am using Async's Library Parallel method wrong? But how? 在 Node JS 中,我有一个无法解析的异步操作。 如何使用工作线程中止它? - In Node JS, I have an async operation that does not resolve. How do I use worker thread to abort it? 如何使用Babel 7的插件提议装饰器和异步函数来装饰异步方法? - How do I decorate an async method using Babel 7's plugin-proposal-decorators with an async function? 如何使用Promises执行后台异步任务 - How can I do a background async task using Promises
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM