stencil icon indicating copy to clipboard operation
stencil copied to clipboard

bug: `connectedCallback` does not work as expected when `async`

Open jgroth opened this issue 2 years ago • 3 comments

Prerequisites

Stencil Version

v2.15.0

Current Behavior

According to the documentation, connectedCallback will run before componentWillLoad. However, when connectedCallback is async it will not wait for the promise from connectedCallback before running componentWillLoad. I'm not sure if this should work or not since the documentation is a bit vague. For connectedCallback it says

This lifecycle hook follows the same semantics as the one described by the Custom Elements Spec

But further down it also says

Lifecycle methods can also return promises which allows the method to asynchronously retrieve data or perform any async tasks. A great example of this is fetching data to be rendered in a component

Expected Behavior

Either componentWillLoad should not run until connectedCallback has resolved its promise when it is async, or the documentation should be more clear on how this is supposed to work.

Steps to Reproduce

Clone the repo and npm start to see the behavior

Code Reproduction URL

https://github.com/jgroth/stencil-connected-callback

Additional Information

Might be related to #2954

jgroth avatar Apr 12 '22 09:04 jgroth

Thanks for the report! I'm going to label this to get it ingested to get looked into further

rwaskiewicz avatar Apr 12 '22 17:04 rwaskiewicz

It's not super explicit from the reproduction code, but note that the value 'init' is never rendered, because the render method does wait for the async componentWillLoad. It's just the async componentConnected that it doesn't wait for.

adrianschmidt avatar Apr 13 '22 13:04 adrianschmidt

Stencil exports the following ComponentInterface:

export interface ComponentInterface {
    connectedCallback?(): void;
    disconnectedCallback?(): void;
    ...
}

So I think this issue can be resolved by simply changing the docs from:

Lifecycle methods can also return promises which allows […]

to:

Some lifecycle methods can also return promises which allows […]

adrianschmidt avatar Jul 08 '22 09:07 adrianschmidt