如何在 async.eachSeries 回调函数中调用回调函数
How can I call callback function in async.eachSeries callback function
我试图在 eachSeries
完成后调用回调函数,但它根本不起作用。它不打印 2
应该在调用时打印,但它在调用第一个函数后打印 4
。有什么想法吗?谢谢!
async.waterfall([
function(callback) {
console.log("1");
let eroJson = [];
rp(optForReddit).then(function(redditJSON) {
let posts = redditJSON.data.children;
async.eachSeries(posts, function(item, callback) {
if (isVideo(item.data.url)) {
eroJson.push(getAlbumId(item.data.url));
}
callback(); // callback function after eachSeries
}, function() {
callback(eroJson); // call next callback
});
})
},
function(redditJSON, callback) {
console.log("2");
callback() // call another function
}
)],
function(){
console.log("Last one");
}
);
更改回调名称。看起来你正在覆盖回调名称
async.waterfall([
function(waterfallCallback) {
console.log("1");
let eroJson = [];
rp(optForReddit).then(function(redditJSON) {
let posts = redditJSON.data.children;
async.eachSeries(posts, function(item, callback) {
if (isVideo(item.data.url)) {
eroJson.push(getAlbumId(item.data.url));
}
callback(); // callback function after eachSeries
}, function() {
waterfallCallback(eroJson); // call next callback
});
})
},
function(redditJSON, waterfallCallback) {
console.log("2");
waterfallCallback();
}
)], function(){
console.log("Done executing all waterfall functions");
});
我试图在 eachSeries
完成后调用回调函数,但它根本不起作用。它不打印 2
应该在调用时打印,但它在调用第一个函数后打印 4
。有什么想法吗?谢谢!
async.waterfall([
function(callback) {
console.log("1");
let eroJson = [];
rp(optForReddit).then(function(redditJSON) {
let posts = redditJSON.data.children;
async.eachSeries(posts, function(item, callback) {
if (isVideo(item.data.url)) {
eroJson.push(getAlbumId(item.data.url));
}
callback(); // callback function after eachSeries
}, function() {
callback(eroJson); // call next callback
});
})
},
function(redditJSON, callback) {
console.log("2");
callback() // call another function
}
)],
function(){
console.log("Last one");
}
);
更改回调名称。看起来你正在覆盖回调名称
async.waterfall([
function(waterfallCallback) {
console.log("1");
let eroJson = [];
rp(optForReddit).then(function(redditJSON) {
let posts = redditJSON.data.children;
async.eachSeries(posts, function(item, callback) {
if (isVideo(item.data.url)) {
eroJson.push(getAlbumId(item.data.url));
}
callback(); // callback function after eachSeries
}, function() {
waterfallCallback(eroJson); // call next callback
});
})
},
function(redditJSON, waterfallCallback) {
console.log("2");
waterfallCallback();
}
)], function(){
console.log("Done executing all waterfall functions");
});