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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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.