Can you make the Ionic lifecycle methods (e.g, `ionViewWillEnter`) async?

Just tried it: declared

    public async ionViewWillEnter(): Promise<void> {

in my component, instead of the usual

    public ionViewWillEnter(): void {

… because in this case the code is easier to follow with the async / await way of writing it.

ionViewWillEnter seems to work just fine when declared async - it still gets called and runs as it used to.

If there is anything wrong that you see or any risk with declaring the Ionic lifecycle methods as async, please holler, thanks!

Only thing I can think of is that it could conflict with coding standards that mandate importing the interfaces that declare these lifecycle events. Other than that, though, a function that returns void has effectively declared that nobody can do anything with its return value, ergo it can return whatever it wants to - nobody is listening. As a reader I would also find it a bit unsettling that the async doesn’t really do anything.

1 Like

You are right. It’s possible that when Ionic framework calls ionViewWillEnter() it expects the call to end before it does anything else… Such a time related expectation should not be ignored by putting an async in front of ionViewWillEnter(). Thanks!

FWIW, that isn’t really what I meant. In fact, what you describe here is specifically not possible - when the function is declared to return void, that means that nobody can make any assumptions about what is in fact returned from it.