5

I am using exec() to run a terminal command to show dialog in electron mac application.

The code I am using:

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

request('https://server_url', function (error, response, data) {

    console.log("inside request");

    exec(`osascript -e 'with timeout of 86400 seconds
                                    tell app "System Events"
                                        display dialog "` + data.pop_up_message + `" buttons {"OK", "Cancel"} 
                                    end tell 
                                end timeout'
    `, function(error, stdout, stderr){

        console.log("inside exec");

    });

});

Its showing multiple dialogs in a single request.

Console output:

inside request
inside exec
inside exec
inside exec

Here 'inside request' is getting printed only once. But the 'inside exec' is getting printed multiple times. What is the reason for this issue. How can I solve this.

Arun D
  • 2,369
  • 5
  • 27
  • 39

1 Answers1

0

The inner function is a callback that is connected to 2 streams: stdout and stderr. The third parameter is an error object that typically contains something if the child process failed or timed out (see: documentation).

Whenever you exec something, his output will arrive after some time by sending messages through one of these. It can happen quite shortly after, or a very long time after. Also, it can be a rather small or big output, so this will come into chunks of data depending on what the execution does. The "under the hood" of stdout and stderr can be easier to visualize in the C / C++ perspective.

Let's say that you write a C program that prints to console every 5 seconds by writing to stdout and then you call it with your NodeJS module.

Your console.log here will print something as soon as it detects a message being sent through one of these streams. In that specific case, it will print every 5 seconds.

Just a small picture, since there could be a lot more to say about what streams are and how they work. This answer adds some interesting details about them.


Update - 29/18/2016:

Updated the streams part. Like Jaromanda X suggested, error isn't a buffer but rather and Error object.

Community
  • 1
  • 1
Frederik.L
  • 5,522
  • 2
  • 29
  • 41
  • error isn't a stream, is it? I know what stdout is, I know what stderr is ... but error? [documentation](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback) doesn't say anything about being called for each stream - in fact, there are no streams involved ... just an Error object and two Buffers – Jaromanda X Aug 29 '16 at 05:27
  • You might be right. Im still looking for an evidence on what it is exactly. I think this is a message that NodeJS sends himself, which is (I think) a stream. It would be interesting to clarify this. – Frederik.L Aug 29 '16 at 05:31
  • I won't take a chance on this, an error object is safer for now. Thanks for pointing this out! Will update. – Frederik.L Aug 29 '16 at 05:33
  • But not only the 'inside exec' is running multiple times. The dialog is also shown multiple times. So the exec command is also running multiple times. – Arun D Aug 29 '16 at 05:36