我正試圖在一個對象數組上運行異步循環async.each
。 在數組中的每個對象上,我試圖按順序運行兩個函數(使用promises
)。問題是async.each
僅運行第一個關鍵字。async.each在使用promise時不會迭代
在以下代碼中,getKeywords
從文件加載一些關鍵字,然後返回一個關鍵字對象數組。每個關鍵字對象都被放到searchKeyword
中進行搜索。然後使用InsertSearchResults
將搜索結果放入數據庫。
在我看來,每個關鍵字應該被並行處理,並且搜索和插入功能被鏈接。
getKeywords(keys).then(function(keywords) {
async.each(keywords, function(keywordObject, callback) {
searchKeyword(keywordObject).then(function(searchResults) {
return insertSearchResults(searchResults, db, collections);
}).then(function(result) {
console.log("here");
callback();
})
})
})
你可以嘗試調用捕獲'err'的可選回調嗎? –