0

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?

1 Answer 1

1

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);
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

I use generator to wrap all the code into Promise, and run it as 'this.body = yield promise'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.