-2

my objective it to get the result from getGpsData() in result1 or it would be great if i could print result1 data in result2

var serialport = require('serialport');
var gps = require('./GPS_SerialPort.js');

processGpsData();

function processGpsData() {

    gps.getGpsData().then(result => {
        console.log("result1: "+result); // need result here
    });
    console.log("result2: "+result); // need result here

}

file 2.

var serialport = require('serialport');

module.exports = {

    getGpsData:async () =>{
        var port = new serialport("/dev/ttyACM0",{baudRate: 9600});
        port.on('open', function () {

            process.stdin.resume();
            process.stdin.setEncoding('utf8');
        });

        port.on('error', function (err) {
            console.error(err);
            process.exit(1);
        });



        var test="";
        var counter=0;
        port.on("data",function(data){
            counter++;
            test +=data;

            if(counter>30){
                console.log("test1:  "+test);
                // return test; //need return this, but not working
                port.close();
                // resolve(test);
                return test;
            }
        });
    },
};
deceze
  • 510,633
  • 85
  • 743
  • 889
ipin
  • 9
  • 2
  • @amadan, please kindly help me remove this from duplicate, as i cant see anything could me in those 2 link. in fact, i had already found those 2 link even before i post this question here. im not an expert in javascript. and maybe that is why i do not understand well if there is an answer in those 2 link provided. however from what i could see. it quite different. please kindly correct me if im wrong here – ipin Jun 13 '19 at 06:39
  • On second look, you are correct - async problems are just a part of your problem. If you're okay with getting data in `result1` (but not in `result2`), that is a separate issue (but still connected to the links I posted as duplicate.) – Amadan Jun 13 '19 at 06:55

1 Answers1

2

You have several problems here, most of them relating to asynchronicity. Before your edit, I closed it with canonical asynchronicity questions, as there is no way to get result at result2 location.

You still have asynchronicity issues, just elsewhere. Your port.on(...) callback is returning a value you want. That value is never going to be used. On the other hand, the async function getGpsData does not have any return of its own, and thus will pass nothing when its promise is resolved (i.e. .then(result => ...) will receive nothing.

The easiest way to do this is to explicitly handle the promise that getGpsData returns. You have the correct idea that you want to resolve(test) and not return test, but resolve is not defined, because you don't have the relevant promise.

This is what you need to do:

return new Promise(resolve => {
      port.on("data",function(data){
        counter++;
        test +=data;

        if(counter>30){
            console.log("test1:  "+test);
            port.close();
            resolve(test);
        }
    });
});

Since your getGpsData does not contain await and is manually handling its promise, you don't need async keyword. When called, getGpsData will construct a promise, which will resolve after 30 triggers on port, and .then(result) in main code will receive the passed argument. As discussed in How do I return the response from an asynchronous call?, it is impossible for this value to be available at result2.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Thanks alot dude, im not proficient with the promise and resolve thing yet. thus having many attempt of failing. its nice to know that promise could be used this way. Thjanks alot – ipin Jun 19 '19 at 02:43