Skip to content
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

Closed
juice49 opened this issue Jan 7, 2015 · 4 comments
Closed

Is it possible to defer a request in beforeSend or ajaxConfig? #34

juice49 opened this issue Jan 7, 2015 · 4 comments
Labels

Comments

@juice49
Copy link

juice49 commented Jan 7, 2015

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 or beforeSend waited for a callback to continue, I could first fetch my access token, set it for the pending request and continue.

@bear bear added the question label Jan 7, 2015
@herkyl
Copy link

herkyl commented Feb 25, 2015

Any updates on this? I have the same issue where I occasionally need to fetch a new access token.

@codepunkt
Copy link

Same here.

@naugtur
Copy link
Member

naugtur commented Jul 11, 2015

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)
  }
}

@juice49
Copy link
Author

juice49 commented Jul 12, 2015

@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! 😊

@juice49 juice49 closed this as completed Jul 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants