JohnnyHK zwrócił uwagę na mój błąd. Korzystając z biblioteki, Async - dostosowałem kod, aby odzwierciedlić jego odpowiedź:
function insertFeedItems(feedItems,newFeedID){
async.eachSeries(feedItems, function(item, eachCallBack) {
let firstName = item.author.substr(0, item.author.indexOf(' '));
let lastName = item.author.substr(item.author.indexOf(' ') + 1);
async.waterfall([
(callback) => {
authorController.createAuthorInternally(firstName, lastName, function(author) {
return callback(null, author, item.categories);
});
},
(author, categories, callback) => {
categoryController.createCategoryInternally(categories, function(categories) {
return callback(null, author, categories);
});
},
(author, categories, callback) => {
feedEntryController.createFeedEntry(item, author, categories, function(entry) {
return callback(null, author, categories, entry);
});
},
],
function(waterfallError) {
if(!waterfallError){
eachCallBack();
}
});
}, function(eachSeriesErr) {
if(eachSeriesErr) {
console.log('An item failed to process');
} else {
console.log('All items have been processed successfully');
}
});
}