-
Notifications
You must be signed in to change notification settings - Fork 28
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
Is it possible to defer a request in beforeSend or ajaxConfig? #34
Comments
Any updates on this? I have the same issue where I occasionally need to fetch a new access token. |
Same here. |
Hi, I'm one of the maintainers of xhr and recently a contributor here. beforeSend is available in ampersand-sync mostly as a legacy from backbone/jQuery.ajax and the original one is also synchronous. I'm pretty sure that for the sake of adoption etc. it'd be better if we didn't start requiring it to call a callback when done. (for the same reason I would consider aborting on return false naugtur/xhr#83) It's been a while and you probably found a workaround. Would you like to share? It could probably be hacked like this (not tested): beforeSend: function(xhr){
var origSend = xhr.send;
xhr.send=function(body){
//do async magic, and then call:
origSend.call(xhr,body)
}
} |
@naugtur Ah, that looks perfect! Thank you! My workaround is very inelegant; it involves occasionally requesting a new access token while the client has a valid refresh token. The logic to do this in an offline available single page app with many forms was quite horrible! Switching to this solution will eliminate a bunch of code and hoops to jump through. Thanks for for the help, it is much appreciated! 😊 |
I have had a look at the source for ampersand-sync and xhr, and it seems that ultimately when
beforeSend
is called in xhr, no control over when the request is actually sent is ceded to it.I suspect this is something that is related to the xhr module, rather than ampersand-sync, but I wonder if anybody has encountered this before.
My specific use case is that occasionally I need to fetch a new access token from an API before a request is made. If either
ajaxConfig
orbeforeSend
waited for a callback to continue, I could first fetch my access token, set it for the pending request and continue.The text was updated successfully, but these errors were encountered: