0

The line of code below provides and output through the console but I want to assign the console.log data to a variable Below is a sample code

  const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
const port = new SerialPort('COM1', { baudRate: 9600 })

const parser = new Readline()
port.pipe(parser)



parser.on('data', line => variableName = `> ${line}`);

console.log(variableName);
  • 1
    Does `line => variableName = \`> ${line}\`` not work? – John Montgomery Jun 11 '20 at 23:38
  • 2
    Doing this requires understanding of variable scope and the asynchronous nature of callbacks. [This guide](https://www.pluralsight.com/guides/javascript-callbacks-variable-scope-problem) explains it very nicely. – Ryan Jun 11 '20 at 23:40
  • @JohnMontgomery confirmed but does not work ! – abubakar Jun 11 '20 at 23:45
  • What do you mean exactly? Are you getting an error? Is something unexpected happening? – John Montgomery Jun 11 '20 at 23:49
  • @JohnMontgomery yeah am getting an error "variableName not defined" – abubakar Jun 11 '20 at 23:51
  • You need to show the larger code context so we can see what you're really trying to accomlish with that data. As it stands here, this is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) where you haven't described what you want to accomplish and have only given us a tiny piece of context around one attempted solution which means we can't answer the real problem here. – jfriend00 Jun 11 '20 at 23:53
  • Chances are you either need to add this data to an array of data that you are accumulating and then process the array on some sort of `end` event for the parser (which kind of negates the whole advantage of doing line by line processing in the first place) or you need to just process each line inside this callback. Asynchronously retrieved data generally shouldn't be assigned to a higher scope (as this is often the source of programming problems when people misunderstand how asynchronous coding works). – jfriend00 Jun 11 '20 at 23:57
  • https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – John Montgomery Jun 12 '20 at 00:02
  • @JohnMontgomery have edited the question to show full code, All i want is to get the output data from serial and assign it to a variable then forward it as json – abubakar Jun 12 '20 at 00:02
  • 2
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Jared Smith Jun 12 '20 at 00:09

1 Answers1

2

This function

line => variableName = `> ${line}`

isn't getting invoked, because the program hasn't gotten to the next tick of the event loop before you're calling console.log. This will never work. If you want to log that outcome, log it inside the .on callback

line => {
  const variableName = `> ${line}`;
  console.log(variableName);
}

I wrote it like that because the implicit return from the un-bracketed arrow function wasn't returning to anything anyway.

EddieDean
  • 1,806
  • 11
  • 13