在 async.js 瀑布方法的成功回调中获得响应
Getting response in success callback of async.js waterfall method
我使用 async.js
waterfall
方法有以下功能:
async.waterfall([
funcOne,
funcTwo,
function upload(items, next) {
async.each(items,
function(item, callback) {
s3.putObject({
Bucket: srcBucket,
Key: item.key,
Body: item.data,
ContentType: item.contentType,
Metadata: {
type: 'thumbnail'
}
}, function(err,data){
callback(err,data);
});
},
function(err,response) {
next(err,response);
});
}],
function(err,response) {
if (err) {
console.error(err);
}
else {
console.log('------ SUCCESS',response);
}
}
);
我想访问 waterfall
的 success
回调中的 async.each
响应。
我尝试了几种不同的做法 callback(err,data) => next(err,response)
并且 ------ SUCCESS
总是 undefined.
我错过了什么?
显然我缺少的是使用 async.map()
而不是 async.each()
。 async.each()
并不像人们期望的那样 return 值。使用 async.map()
会给你一个 return 响应,这样你就可以在链中传递值。
我使用 async.js
waterfall
方法有以下功能:
async.waterfall([
funcOne,
funcTwo,
function upload(items, next) {
async.each(items,
function(item, callback) {
s3.putObject({
Bucket: srcBucket,
Key: item.key,
Body: item.data,
ContentType: item.contentType,
Metadata: {
type: 'thumbnail'
}
}, function(err,data){
callback(err,data);
});
},
function(err,response) {
next(err,response);
});
}],
function(err,response) {
if (err) {
console.error(err);
}
else {
console.log('------ SUCCESS',response);
}
}
);
我想访问 waterfall
的 success
回调中的 async.each
响应。
我尝试了几种不同的做法 callback(err,data) => next(err,response)
并且 ------ SUCCESS
总是 undefined.
我错过了什么?
显然我缺少的是使用 async.map()
而不是 async.each()
。 async.each()
并不像人们期望的那样 return 值。使用 async.map()
会给你一个 return 响应,这样你就可以在链中传递值。