【发布时间】:2015-03-02 02:39:54
【问题描述】:
我需要发出几个 API 请求,然后对组合结果集进行一些处理。在下面的示例中,您可以看到通过复制相同的请求代码发出了 3 个请求(到 /create),但是我希望能够指定要发出多少个请求。例如,我可能希望运行相同的 API 调用 50 次。
如何在不重复n次API调用函数的情况下进行n次调用?
async.parallel([
function(callback){
request.post('http://localhost:3000/create')
.send(conf)
.end(function (err, res) {
if (err) {
callback(err, null);
}
callback(null, res.body.id);
});
},
function(callback){
request.post('http://localhost:3000/create')
.send(conf)
.end(function (err, res) {
if (err) {
callback(err, null);
}
callback(null, res.body.id);
});
},
function(callback){
request.post('http://localhost:3000/api/store/create')
.send(conf)
.end(function (err, res) {
if (err) {
callback(err, null);
}
callback(null, res.body.id);
});
}
],
function(err, results){
if (err) {
console.log(err);
}
// do stuff with results
});
【问题讨论】:
标签: node.js function asynchronous code-duplication