0

I am reading a package.json file of a project, and trying to identify if the next framework dependency exists under my dependencies key. And everything is alright, the console.log('We made it') that you can see works, when I invoke it with functionIamStuckWith;

What I want to get out of this is a boolean value that is either true or false, to use it in a condition and do things conditionally. That is why I return true or false.

I tried and I couldn't make it, I created a private (also tried public) isNextJsFramework: boolean;, then initialized it in the constructor and in my promise function I tried to set the field like this.isNextJsFramework = true. What I got out of it tho, is my Visual Studio Code warning me that 'isNextjsFramework' is declared but its value is never read..

Do u have any idea how can I fix the syntax? Or why this doesn't work?

class Parser {
  private options: ParserOptions;
  private isNextJsFramework: boolean;
  // Also tried to initialize it here
  // private isNextJsFramework = false;

  constructor(options: ParserOptions, isNextJsFramework: boolean) {
    this.isNextJsFramework = isNextJsFramework;
    this.options = options;
  }

  public parse() {
    return new Promise((resolve) => {    
      const functionIAmStuckWith = this.getPackageJson().then(async (path: string[]) => {
        const packageJsonFile = await JSON.parse(
          fs.readFileSync(path[0], 'utf8')
        );

        if (packageJsonFile) {
          if (packageJsonFile.dependencies) {
            Object.keys(
              packageJsonFile.dependencies).forEach((dependency) => {
              if (dependency.match(/^next$/)) {
                // I get the error when I use it here.
                // If I just type this.isNextJsFramework without assigning a value, it does not complain
                console.log("We made it");
                this.isNextJsFramework = true;

              }
            });
          }
        }
      });


      functionIamStuckWith;

      ......... More here and at some point it is resolved
botana_dev
  • 418
  • 4
  • 16
  • Please do not just delete and re-post your [previous question](https://stackoverflow.com/q/75844415/1048572) after it has been closed. Instead, [edit it](https://stackoverflow.com/posts/75844415/edit) if you need further clarifications. – Bergi Mar 25 '23 at 22:51
  • "*What I want to get out of this [method] is a boolean value*" - as previously stated, this is **impossible** if your method is asynchronous. The best you can do is to return a promise, and wait for the result where you are calling it. Please read the canonical question on the topic and its answers to understand why. – Bergi Mar 25 '23 at 22:53
  • The question is literally asking something else, which is "Why am I getting a VISUAL studio code error" when I use the field in the class, as I already solved it with another way using promises. And the only reason u post a comment is to increase reputation. Instead of doing that, you could be helpful and evaluate a post instead of wasting your time :) – botana_dev Mar 25 '23 at 23:17
  • Oh, sorry, the question was so similar that I assumed the gist was the same... – Bergi Mar 25 '23 at 23:41
  • 1
    You're getting that *warning* (it's not an error, the code compiles and runs) because you never *use* the value that you write into the property. Once you actually access it (e.g. in the condition that you mentioned), the warning would be gone. Alternatively, just remove the class property from your code, or disable that linter rule. – Bergi Mar 25 '23 at 23:43

0 Answers0