So basicly i have a function, that’s starts OAuth flow (redirecting to google oauth login page for example):
async function signIn() {
// start Oauth flow
}
and for instance i want to use it in useEffect like this:
...
useEffect(() => {
async function someAsyncFunc() {
randomFunc1();
await signIn();
randomFunc2();
}
someAsyncFunc();
}, []);
...
in this case randomFunc2()
will never occur because signIn()
will redirect to another url
So my question is how i can tell for other developers that there’s no point to call other functions after signIn()
, because they will never be executed. I heard in typescript there is type called never
, but i’m not sure if this gonna work in my case.
Edit: as example redirect
function in nextjs navigation returns never
Any sugestions?
2
Answers
Change function name so that it is clear that you will do sign in and redirect.
This should work but doesn’t due to a typescript bug
But currently this doesn’t create errors as expected due to this TypeScript bug
Workaround
There is no real neat solution that works right now, but you can at least prevent the most common mistake (code not running that should be run) by just not returning a promise if it will never complete
signIn
now does not return a promise, so it can’t be awaited. This way it is impossible to do something aftersignIn
completes.You can still write code in the lines afterwards but they will execute like normal, not wait will the sign in completes.
In your example:
randomFunc1
andrandomFunc2
will both run as intended, regardless or where you placesignIn
Note this only works if
randomFunc1
etc. are not async otherwise they may not complete before the pages closes.