async.each not iterating when using promises

霸气de小男生 提交于 2019-11-27 08:51:23

问题


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

回答1:


You are only using .then() callbacks so you handle success.

But you should also add some .catch() callbacks to handle errors.

Most likely you get errors that are not handled and nothing happens.

For example:

            // ...
        }).then( function(result) {
            console.log("here");
            callback();
        }).catch(function (error) {
            console.log('Error:', error);
            callback(error);
        });



回答2:


It turns out that that I made an error in the getKeywords function. I was reading from a file, then iterating through each line by using a for loop and pushing the result to an array. This array was then returned by the function.

async.each was working perfectly but was only ever receiving an array of length 1 to iterate over.

I fixed this problem by changing the for loop to an async.each loop

function getKeywords(keywordsFilename){
    //get keywords from the file
    return new Promise( function (resolve, reject) {
        var keywords = [];
        fs.readFile(keywordsFilename, function read(err, data) {
            if (err) {
                reject(err);
            }
            content = data.toString();
            var lines = content.split("\n");
            async.each(lines, function(line, callback) {
                if (line[0] === "#" || line == "") {
                    callback();
                }
                else {
                    keywords.push(extractKeyword(line));
                    callback();
                }
            }, function (err) {
                resolve(keywords);
            });
        });
    });
}

Writing the problem out helped, let me know if I should delete the question.

Thanks for your help Mukesh Sharma and rsp.



来源:https://stackoverflow.com/questions/41567353/async-each-not-iterating-when-using-promises

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!