The problem of climbing multiple web pages with nodejs request module?

  node.js, question

I now have many web pages to crawl. I have crawled down these addresses and put them in an array of inside. Then I want to crawl these web pages using the for loop. The code is as follows:

db_operation.db_getUrl('appsCategories_China', function(results){
            for(var i = 0; i < results.length; i加加)
            {
                var item = results[i];
                // console.log(item);
                request(item.url, function(err, res, content){
                    if(!err && res.statusCode == 200)
                    {
                        $$ = cheerio.load(content);
                        getAppsIndex('#selectedcontent', 'a', function(index){
                            console.log(i);
                            index.push(item.category);
                            console.log(item.category);
                            // db_operation.db_addIndex('appsIndex_China', index)
                        });
                    }
                });
            }
}

Results The output of the last two consoles is the last element of the array Results, which should be due to the asynchronous execution of the request, resulting in the completion of the for loop, and the request has not yet returned. May I ask if I should not use the for loop? Ask for guidance!

It is ok to use circulation, sit down and modify ~ ~
Use closures to encapsulate variables

db_operation.db_getUrl('appsCategories_China', function(results){
    for(var i = 0; i < results.length; i加加)
    {
        var item = results[i];
        (function(i,item ){
             request(item.url, function(err, res, content){
                if(!err && res.statusCode == 200)
                {
                    $$ = cheerio.load(content);
                    getAppsIndex('#selectedcontent', 'a', function(index){
                        console.log(i);
                        index.push(item.category);
                        console.log(item.category);
                        // db_operation.db_addIndex('appsIndex_China', index)
                    });
                }
            });
        }(i,item ));
    }
}