2

I am new to javascript. I have a js file something like below(test.js):

async function getData() {
   let a =100;
   // Some other function calls which modifies a
   // Some more console logs here.
   return a;
}
(async () => {
  console.log(await getData()); // When trying to return await getData() giving some error.
})();

I am executing the above script output = $(node test.js) in shell script and I need the value of a in the shell script but here I am getting all the console.log statements. Is it possible to get the value?

5
  • check this answer maybe help you Commented Aug 22, 2021 at 8:37
  • I tried that, but it also printing all console log statements along with the returned value. Commented Aug 22, 2021 at 8:44
  • is it because of the spaces? shell is very picky about spaces, pls make sure it's output=$(node test.js) and there is no space around the = sign Commented Aug 22, 2021 at 8:58
  • Have a look at process.stdout. With stdout you print out stuff so that it can be used by other application. Maybe this can also help: bash - How to handle stdout in node.js - Stack Overflow Commented Aug 22, 2021 at 9:08
  • can you add shell script to question? Commented Aug 22, 2021 at 9:16

1 Answer 1

0

If you know that output will always have one line (by output I mean returned value from getData()), you can use $(node test.js | tail -n 1) to grab only one last line from node test.js. Otherwise you can do this little hacky workaround by replacing console.log function:

async function getData() {
   let a = 100;
   console.log("Some cosole log");
   console.log("Some other log");
   return a;
}
(async () => {
        console_log = console.log; // Save original function for later
        console.log = ()=>{}; // Replace original with "dummy" function
        console_log(await getData()); // Use saved original to log only what we want
})();

This will work unless some part of code use process.stdout.write to log/print data directly. To avoid this, we can again replace process.stdout.write with some dummy function but since this is likely to break some other things, I would avoid it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response. I can use one of them. I am more inclined to $(node test.js | tail -n 1)
How to get the data if the variable returned in multi-line string variable?
If I try to use console_log = console.log; it's saying void function return value is used.
I assume it's just a warning from the IDE so it will be fine at runtime (as in JS you are allowed to use "void values" (functions without a return statement) as assignments). If this message bother you (like it would bother me), you can probably find some way to suppress this warning/error.

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.