Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

typescript method returning promise doesnt return a object always.

Posted on 2018-01-13
6
Medium Priority
?
26 Views
Last Modified: 2018-01-17
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

0
Comment
  • 3
  • 3
6 Comments
 
LVL 83

Expert Comment

by:leakim971
what did it return when it's not an object ? undefined ?
0
 

Author Comment

by:Srilalitha vishnubhotla
yes undefined.
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
linereader is asynchronous, you should use promise not at the end , check promisify : https://github.com/nickewing/line-reader#promises
1
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Srilalitha vishnubhotla
Changed code.  returns promise in the beginning of function.  It can be closed.
0
 
LVL 83

Expert Comment

by:leakim971
if your issue is solved, please close your question by selecting ans accept one or more answer as solution.
0
 

Author Closing Comment

by:Srilalitha vishnubhotla
Issue is closed
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question