-1

Nevermind if you disapprove what I'm doing seems overcomplicated but it's an experiment to undersyand, my question here is only about syntax : I can't see what's wrong since https://jsfiddle.net/tx2ac6kq/28/ outputs no error but I should have log from checkText and I don't get any :

class Test {
  #text; 

  set text(text) {
    this.#text = text;
  }
  

  get text() {
      console.log("get text")
      let value = (async () => await new Promise(resolve => {
          let timerId = setInterval(checkText, 1000);
          checkText = () =>  {
          console("check")
          let value = this.#text;
          console.log(value);
          
          if (value) {
            clearInterval(timerId);
            resolve(value);
          }
          else {
            console.log("checkText");
            reject("checkText rejected");
          }
        }
      }))();
      
  }
  
  constructor() {
    fetch("https://jsonplaceholder.typicode.com/posts/1")
      .then((response) => {
        response.text().then((response) => {
          //console.log("response", response);
          this.text = response;
          console.log("this.text", this.#text);
        });
      });
  }
}

let test = new Test();
let value = test.text;
user310291
  • 35,095
  • 76
  • 252
  • 458
  • 3
    Do read error messages. `Uncaught ReferenceError: checkText is not defined` You don't have `checkText` as a function declaration, and it's defined after the setInterval call, so it's not defined in time – CertainPerformance May 12 '22 at 21:33

0 Answers0