Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awaiting in onCreate doesn't work as expected #1722

Closed
treeder opened this issue Jul 28, 2021 · 4 comments
Closed

Awaiting in onCreate doesn't work as expected #1722

treeder opened this issue Jul 28, 2021 · 4 comments
Labels
type:feature A feature request

Comments

@treeder
Copy link

treeder commented Jul 28, 2021

I'm trying to fetch some data to put in the state before rendering, but I can't seem to make it work. I've tried the following:

class {
  async onCreate(input) {
    console.log("onCreate");
    this.state = {
       something: null,
    }
    let somethingF = api('get', `https://somewhere.com/${input.params.id}`)
    let something = await somethingF
    console.log("something:", something) // this prints to console OK
    this.state.something = something
  }
}

// later in the code
<script>
console.log("in JS", ${JSON.stringify(state)}) // this prints the state with `something: null`
</script>

I also tried:

class {
  async onCreate(input) {
    let somethingF = api('get', `https://somewhere.com/${input.params.id}`)
    let something = await somethingF
    console.log("something:", something) // this prints to console OK
    this.state = {
       something: something,
    }
  }
}
// later in the code
<script>
console.log("in JS", ${JSON.stringify(state)}) // this prints `undefined`
</script>

So it seems that the state will not be set properly after an await in onCreate. I feel like it should let onCreate complete fully before continuing so the state can be setup properly. Is that now how it's intended?

@treeder treeder added the type:feature A feature request label Jul 28, 2021
@taylor-hunt-kr
Copy link

Usually the <await> tag is how this sort of thing is done; does it work for you here?

@treeder
Copy link
Author

treeder commented Jul 28, 2021

I tried that, but then I have to put everything like scripts and what not underneath the await tag. I want (at least I think I want) the data available for the entire page, like way down in the script section. Unless I'm just thinking about this stuff all wrong... ?

@treeder
Copy link
Author

treeder commented Jul 28, 2021

Ok, so that works if I move the script into the await block.

But I can't use onCreate due to the #942 so I guess that means I can't use/update state?

image

@DylanPiercey
Copy link
Contributor

DylanPiercey commented Jul 29, 2021

@treeder the async logic of your app is currently intended to be executed on the server side only with Marko.

If you have some stateful logic what will work best is to move that lower in the component tree, such that the higher components (with async content) can be bundled and resolve only on the server.

The lower we can move state in the tree, the fewer components that need to be shipped to the browser, so it typically ends up being a perf win also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature A feature request
Projects
None yet
Development

No branches or pull requests

3 participants