我不能为我的生活找到答案.如何使用caolan的async.js节点模块将参数传递给async.each的迭代器函数?我想重用迭代器,但它需要根据上下文使用不同的前缀保存.我得到的是:
async.each(myArray, urlToS3, function(err){
if(err) {
console.log('async each failed for myArray');
} else {
nextFunction(err, function(){
console.log('successfully saved myArray');
res.send(200);
});
}
});
function urlToS3(url2fetch, cb){
//get the file and save it to s3
}
Run Code Online (Sandbox Code Playgroud)
我希望能做的是:
async.each(myArray, urlToS3("image"), function(err){
if(err) {
console.log('async each failed for myArray');
} else {
nextFunction(err, function(){
console.log('successfully saved myArray');
res.send(200);
});
}
});
function urlToS3(url2fetch, fileType, cb){
if (fileType === "image") {
//get the file and save it to s3 with _image prefix
}
}
Run Code Online (Sandbox Code Playgroud)
我找到了一个与coffeescript相似的问题,但答案没有用.我愿意重构,以防我试图做一些不恰当的事情,但这似乎是一件合乎逻辑的事情.
rob*_*lep 15
您可以使用bind以下命令创建部分函数:
async.each(myArray, urlToS3.bind(null, 'image'), ...);
Run Code Online (Sandbox Code Playgroud)
参数'image'将作为函数的第一个参数传递(其余参数将是传递的参数async),因此它看起来像这样:
function urlToS3(fileType, url2fetch, cb) {
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3137 次 |
| 最近记录: |