0

      class Demo {
        constructor() {
          this.state = []

          this.init()
        }

        init() {
          fetch('https://jsonplaceholder.typicode.com/todos/1')
            .then((response) => response.json())
            .then((json) => {
              // json.id === 1
              this.state.push(json.id)
            })
        }

        getState() {
          return this.state //
        }
      }

      const demo = new Demo()

      console.log(demo.getState())

when the class was constructed, init() method reset the state array, but demo.getState() return []

how can i get the state by getState method after the http request?

getState() expect --> [1]

0 Answers0