Thursday, 31 August 2017

javascript - async.each not iterating when using promises

I am trying to run an asynchronous loop async.each over an array of objects.
On each object in the array, I am trying to run two functions sequentially (using promises). The problem is that async.each only runs for the first keyword.



In the following code, getKeywords loads some keywords from a file, then returns an array of keyword objects. Each keyword object is put into searchKeyword that makes a search. The search result is then put into a database using InsertSearchResults.



In my mind, each keyword should be processed in parallel and the search and insert functions are linked.




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();
})
})

})

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...