You.Brighton You.Brighton - 2 months ago 11
Node.js Question

node request multi thread issue

I want to process first item('http://www.google.com') array2.foreach complete before start second item('http://www.amazon.com')
but request is support thread so this result is

http://www.google.com
http://www.amazon.com
http://www.apple.com
1
2
.
.


but I want to be

http://www.google.com
1
2
3
.
.
http://www.amazon.com
1
2
.
.


MyCode

var async = require('async');
var request = require('request');

var array1 = ['http://www.google.com', 'http://www.amazon.com',
'http://www.apple.com'];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];

array1.each(function (item, callback) {

console.log(item);
aa(item, callback)

})

function aa(url) {

request(url, function (err, res, html) {
array2.forEach(function (item, callback) {
//This part Current Page crawling
console.log(item);
sleep(1000);
});
})
}


how can I do that?

Answer

You have added async module but not yet used. You need to use it, you may like to use async.eachSeries() function to achieve your need. Here is code that may help you

var async = require('async');
var request = require('request');

var array1 = ['http://www.google.com', 'http://www.amazon.com', 
'http://www.apple.com'];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function aa(url, callback) {

    request(url, function (err, res, html) {
        async.eachSeries(array2, function iteratee(item, callback2) {
            console.log(item);
            setTimeout(function() {
                callback2(null);
            }, 1000);
        }, function done() {
            callback(null)
        });
    })
}

async.eachSeries(array1, function iteratee(item, callback) {
    console.log(item);
    aa(item, callback)
}, function done() {
    console.log('completed');
});