1

I'm working on making a script with node.js for setting up my dzen2 in i3, and haven't really used node for anything like this before.

I need the geometry of the screen to start with, which I can get with something like this:

geometry = getGeo();

function getGeo() {
  var sh = require('child_process').exec("i3-msg -t get_outputs",
    function(error, stdout, stderr) {
      var out = JSON.parse(stdout);
      return out[0].rect; //this is the geometry, {"x":0, "y":0, "width":1280, "height":768}
  });
};

console.log(geometry);

console.log is logging undefined.

I'm not sure what the proper way to do this is, my brain is tired.

2 Answers 2

3

You can't return from the callback function since is async. Rather write another function and pass the callback object to it.

function getGeo() {
var sh = require('child_process').exec("i3-msg -t get_outputs",
    function(error, stdout, stderr) {
      var out = JSON.parse(stdout);
      getRect(return out[0].rect);
    });
};

function getRect(rect) {
    // Utilize rect here...
}
Sign up to request clarification or add additional context in comments.

2 Comments

I've been working with so many different things, and just picking up coldfusion for work, that my brain is explody. Thanks, I remember how to write nodejs again.
haha..Glad it helped. You can accept it if it worked for you.
0

You never return a value from getGeo(), you're returning a function from the anonymous function within it. But because of the async nature of the .exec() call, you can't return the value. You can put the console.log into the callback function, but that may not be where you want to use it in your real program.

2 Comments

So what's the proper thing to do here? return sh from before
node.js is based entirely on asynchronous programming with callbacks. You have to organize your code differently. Synchronous return values from functions are the wrong way to be thinking.

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.