-
Notifications
You must be signed in to change notification settings - Fork 30
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
Use async/await to allow for async storage #61
Conversation
This PR arrives at the right time ! I'm starting a new react-native app that will use SecureStore from Expo, which use async calls, so for now, the custom Storage I made can't works due to the lack of supporting async/await. Hope this will be releases soon :) |
Yep - I made this for Expo SecureStore myself… it works great! You can always directly NPM install from my repo/branch if you want to. That’s how I’m working while I’m waiting on this to be merged. |
Thanks for this work! This will need to be a new major version right, since it will break any existing users? |
// Try to await a current request | ||
if (currentlyRequestingPromise) accessToken = await currentlyRequestingPromise | ||
|
||
if (!accessToken) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can return early here for clarity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't return here - needs to fall through to the code below to set the headers and and the config.
currentlyRequestingPromise = undefined | ||
} catch (error: unknown) { | ||
// Reset the promise | ||
currentlyRequestingPromise = undefined | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can put this in a finally
block, it will always get called
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand? No need for finally
... we're using await
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait - I see what you are saying now. I thought you were referencing waiting on the Promise - but you were actually talking about setting it to undefined
. Sure - that would be fine.
I don't actually know the semantics though - does a finally
get run even if we rethrow like we are on 171? At any rate - I would say to hold off on that change until we get this in. What's here now matches up with the original code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does get called even if rethrowing, I believe the doc I linked is saying that
Not a big deal
No problem! Glad to contribute! A new major version might be a good idea. The upgrade will be pretty easy for most users... they really just need to add an |
Can you bump the major version and we can merge it? |
Redo the way requests queue in the interceptor to remove a race condition. Bump the major version
@revmischa - done! |
Thanks for your work! |
4.0.0 published |
Changed the entire call tree to allow for async storage.
Also: Redid the way requests queue in the interceptor to remove a race condition.
If two interceptors were run back-to-back, it was possible for this to happen:
isRefreshing
- which isfalse
so it skips the queueisRefreshing = true
inrefreshToken
isRefreshing
- which is stillfalse
! So it skips the queueWhy didn't the (well designed!) test catch this before? Because there happened to not be any
await
calls in-between starting the interceptor and settingisRefreshing = true
. However, this was a race condition waiting to happen (which it did - as soon as I added an await!)My fix removes the queue and utilizes the native "queue" created by multiple waiters waiting on the same Promise. This is more robust due to the lack of distance between waiting and setting the Promise - and is quite a bit simpler.