Reputation: 147
I am trying to get data using curl on nodejs. I need to run more than one child_process for each query in order to retrieve the data without delays.
router.post('/curl/', function* () {
var urls = this.request.body.url;
var exec = require('child_process').exec;
var promise = new Promise((resolve, reject) => {
var count = urls.length;
var res = _.map(urls, (stream) => {
// stream = {url:'http://url1.com'};
var command = 'curl '+stream.url+';
exec(command, (error, stdout, stderr) => {
if (error !== null) {
console.log('exec error: ' + error);
}
return stdout;
});
});
resolve(res);
});
this.body = yield promise;
});
This resolve '[null, null]'; I was tried to use Promise.map, but it was failed too.
If I make a single request (without _.map()) - it returns HTML of the requested page, as I expect. How to run more than one child_process for my requests?
Upvotes: 0
Views: 1930
Reputation: 3029
Here is a variant without generator (function*
).
It is unclear what are you trying to do with generator.
var exec = require('child_process').exec;
router.post('/curl/', function() {
function processUrl(url) {
return new Promise((resolve, reject) => {
var command = 'curl ' + stream.url;
exec(command, (error, stdout, stderr) => {
if (error)
return reject(error);
return resolve(stdout);
});
});
}
var urls = this.request.body.url;
var allPromises = urls.map(url => {
return processUrl(url);
});
Promise.all(allPromises)
.then(htmls => {
console.log('HTML of pages:', htmls.length);
});
});
Upvotes: 1