typescript method returning promise doesnt return a object always.

typescript method returning promise after reading data from a file doesn't return object always.

   
 public getData(): Promise<node[] > {
    // read file line by line
    let lineReader = require("line-reader");
    let dataTempJson = "";
    let dataJson = "[{";
    let quote = "\"";
    let cont = -1;
    let contData = -1;
    let cache = "";
    let role = "";
    let resultData;
    try {

        let fs = require("fs");
        const path = require("path");
        lineReader.open(path.join("log", "data.log"), (err: Error, reader: LineReader) => {
            if (err) {throw err; }
            if (reader.hasNextLine()) {
                try {
                    reader.nextLine((error: Error, line: string) => {
                        if (error) {throw error; }
                    });
                } finally {
                    reader.close( (error: Error) => {
                        if (error) {throw error; }
                    });
                }
            } else {
                reader.close((error: Error) => {
                    if (error) {throw error; }
                });
            }
        });
        lineReader.eachLine(path.join("logs", "data.log"), { encoding: "utf8" }, (line, isLast) => {
            let keyLen = line.indexOf(": ") + ": ".length;
            let valueLen = line.indexOf(";");
            cont = line.indexOf("\\");
            contData = line.indexOf(",");
            if (cont > 0) {
                cache = quote + line.substring(0, keyLen - 2).trim() + quote + ":" + quote;
                cont = 0;
            } else if (contData > 0) {
                cache = quote + line.substring(0, keyLen - 2).trim()
                    + quote + ": " + quote + line.substring(keyLen).trim();
            } else {
                let data = "";
                if (cache.length > 0) {
                    data = cache + line.substring(0, line.length - 1).trim() + quote + ",";
                    cache = "";
                } else {
                    let key = quote + line.substring(0, keyLen - 2).trim() + quote;
                    let value = quote + line.substring(keyLen, valueLen).trim() + quote;
                    if (line.substring(0, keyLen - 2).trim()) {
                        data = key + ":" + value + ",";
                    }
                }
                dataTempJson = dataTempJson + data;
                if (line.indexOf("end") >= 0) {
                         dataJson = dataJson
                        + dataTempJson.substring(0, dataTempJson.length)
                        + ipaddress
                        + addParams
                        + "},{";
                    dataTempJson = "";
                    cache = "";
                    contData = 0;
                    cont = 0;
                }
            }
            if (isLast) {
                // tslint:disable-next-line:no-console
                console.log(dataJson.substring(0, dataJson.length - 2) + "]");
                resultData = JSON.parse(dataJson.substring(0, dataJson.length - 2) + "]");
                // tslint:disable-next-line:no-console
                console.log(resultData);
        }
        });
    } catch (error) {
        return error;
    }
    // Return new promise
    return new Promise((resolve, reject) => {
        if (resultData) {
            resolve(resultData);
        } else {
            reject("error");
        }
    });

Open in new window

Srilalitha vishnubhotlaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
what did it return when it's not an object ? undefined ?
0
Srilalitha vishnubhotlaAuthor Commented:
yes undefined.
0
leakim971PluritechnicianCommented:
linereader is asynchronous, you should use promise not at the end , check promisify : https://github.com/nickewing/line-reader#promises
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Srilalitha vishnubhotlaAuthor Commented:
Changed code.  returns promise in the beginning of function.  It can be closed.
0
leakim971PluritechnicianCommented:
if your issue is solved, please close your question by selecting ans accept one or more answer as solution.
0
Srilalitha vishnubhotlaAuthor Commented:
Issue is closed
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.