javascript - child_process exec command is getting executed multiple times -
i using exec()
run terminal command show dialog in electron mac application.
the code 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 single request.
console output:
inside request inside exec inside exec inside exec
here 'inside request'
getting printed once. 'inside exec'
getting printed multiple times. reason issue. how can solve this.
the inner function callback connected 2 streams: stdout
, stderr
. third parameter error object typically contains if child process failed or timed out (see: documentation).
whenever exec something, output arrive after time sending messages through 1 of these. can happen quite shortly after, or long time after. also, can rather small or big output, come chunks of data depending on execution does. "under hood" of stdout
, stderr
can easier visualize in c / c++ perspective.
let's write c program prints console every 5 seconds writing stdout
, call nodejs module.
your console.log
here print detects message being sent through 1 of these streams. in specific case, print every 5 seconds.
just small picture, since there lot more streams , how work. this answer adds interesting details them.
update - 29/18/2016:
updated streams part. jaromanda x suggested, error isn't buffer rather , error object.
Comments
Post a Comment