使用 nodejs 异步和请求模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11062995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:52:42  来源:igfitidea点击:

Using nodejs async and request module

node.jsasynchronoushttprequestasync.js

提问by andrei

I'm trying to use async and request module together but i don't understand how the callbacks get passed. My code is

我正在尝试同时使用 async 和 request 模块,但我不明白回调是如何传递的。我的代码是

var fetch = function(file, cb) {
    return request(file, cb);
};

async.map(['file1', 'file2', 'file3'], fetch, function(err, resp, body) {
    // is this function passed as an argument to _fetch_ 
    // or is it excecuted as a callback at the end of all the request?
    // if so how do i pass a callback to the _fetch_ function
    if(!err) console.log(body);
});

I'm trying to fetch 3 files in order and concatenate the results. My head is stuck in callbacks I tryed and the different combinations I could think of. Google wasn't much help.

我正在尝试按顺序获取 3 个文件并连接结果。我的头脑被我尝试过的回调和我能想到的不同组合所困。谷歌没有太大帮助。

回答by Mustafa

Request is asynchronous function, it does not return something, when its job is done, it calls back. From request examples, you should do something like:

请求是异步函数,它不返回任何东西,当它的工作完成时,它会回调。从请求示例中,您应该执行以下操作:

var fetch = function(file,cb){
     request.get(file, function(err,response,body){
           if ( err){
                 cb(err);
           } else {
                 cb(null, body); // First param indicates error, null=> no error
           }
     });
}
async.map(["file1", "file2", "file3"], fetch, function(err, results){
    if ( err){
       // either file1, file2 or file3 has raised an error, so you should not use results and handle the error
    } else {
       // results[0] -> "file1" body
       // results[1] -> "file2" body
       // results[2] -> "file3" body
    }
});

回答by JohnnyHK

In your example, the fetchfunction will be called three times, once for each of the file names in the array passed as the first parameter to async.map. A second, callback parameter will also be passed into fetch, but that callback is provided by the async framework and you must call it when your fetchfunction has completed its work, providing its results to that callback as the second parameter. The callback you provide as the third parameter to async.mapwill be called when all three of the fetchcalls have called the callback provided to them.

在您的示例中,该fetch函数将被调用三次,一次是针对作为第一个参数传递给的数组中的每个文件名async.map。第二个回调参数也将被传递到fetch,但该回调由异步框架提供,您必须在您的fetch函数完成其工作后调用它,并将其结果作为第二个参数提供给该回调。async.map当所有三个fetch调用都调用了提供给它们的回调时,将调用作为第三个参数提供的回调。

See https://github.com/caolan/async#map

https://github.com/caolan/async#map

So to answer your specific question in the code, the callback function you provide is executed as a callback at then end of all requests. If you need to pass a callback to fetchyou'd do something like this:

因此,要在代码中回答您的特定问题,您提供的回调函数将在所有请求结束时作为回调执行。如果您需要将回调传递给fetch您,请执行以下操作:

async.map([['file1', 'file2', 'file3'], function(value, callback) {
    fetch(value, <your result processing callback goes here>);
}, ...