Reputation: 3
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);
Upvotes: 0
Views: 333
Reputation: 1816
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.
Upvotes: 2