Filippo oretti
Filippo oretti

Reputation: 49817

Node.js exec event not firing inside Promise

I have this simple code which is a promise as you can see:

var exec = require('child_process').exec;

return new Promise(function(resolve, reject) {

        var x = exec(`cd /maps && ls -la`, {}, function(err, stdout, stderr) {

            if (err || stderr) {

              reject(err + stderr);
            }

            resolve(stdout);
          });

          x.stdout.on('data', function (data) {
            console.log(data);
          });
    });

The on event is never fired while if i remove promise from around everything works

do you have any clue?

Upvotes: 1

Views: 498

Answers (1)

rob
rob

Reputation: 156

Try moving your resolve out of your callback function, and call it after console.log(data).

The reason may have been because you were resolving your promise too early.

Upvotes: 1

Related Questions