0

there is a problem with asynchronous processing, but I don't know how to solve it.

I was working on the MVC pattern.

The controller should receive the model and hand over the data to the view, but the data received from the model is displayed as undefined.

here is code.

controller

enter image description here

model

enter image description here

when console.log(model)

enter image description here

If it is OK, the data should be in the list.(in this.#list)

but But as you can see, it is Array(0)

I don't know how to solve it. I spent 2 to 3 hours. please help!

juuu o
  • 33
  • 3
  • 3
    [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Ivar Mar 24 '22 at 22:19
  • Wait for the model to have data before you render the view... (something like `model.loadData().then(() => view.render(model))`) – Heretic Monkey Mar 24 '22 at 22:24
  • Don't try to put asynchronous code in a constructor. Ask yourself do you even have an instance of a controller before that data is loaded? If not, write a static asynchronous function that loads the data and returns an instance of controller after constructing it for you. Otherwise require the user to call loadData on the constructed instance of controller. – Patrick Roberts Mar 24 '22 at 22:25
  • `async` functions synchronously return a pending promise before `await` operations within the function body have completed. Hence the constructor returns before data has loaded. – traktor Mar 24 '22 at 22:33
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – traktor Mar 24 '22 at 22:33

0 Answers0