-
Notifications
You must be signed in to change notification settings - Fork 129
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
fix: allow async functions in beforeSend and afterSend #166
Conversation
Could you elaborate what is your use case for this? |
pb.beforeSend = async (url, option) => {
// Do async stuff here.
await ...
} My use case is to run await a fetch in the beforeSend, but this would allow many more async usecases, without much code change. |
I understand what it does, but I don't know why you would want to run fetch in a In any case, I guess we can allow it but we also need to update the |
We also will need unit tests for the async hook behavior in https://github.com/pocketbase/js-sdk/blob/master/tests/Client.spec.ts#L197-L254 |
I have added passing tests that fail without the change.
My use case is a little bit dumb, but basically my hosting provider goes to sleep after a while of inactivity, and I have a beforeSend hook to check if it's alive and to start it if it's not. However, I believe this is a small change with little maintenance needed, and it allows for more usecases than my own. |
|
I've squash merged it locally with some minor changes in relation to the tests to ensure that the hooks I will push sometime later today v0.13.0-rc release (the stable release will be available most likely together with the PocketBase v0.14.0 release in the next couple weeks). |
This patch allows async functions to be in beforeSend and afterSend to be executed correctly. Sync functions aren't affected because await works for sync functions too.