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

Async transforms #19

Closed
nickdima opened this issue Oct 19, 2014 · 9 comments
Closed

Async transforms #19

nickdima opened this issue Oct 19, 2014 · 9 comments

Comments

@nickdima
Copy link

What do you think about returning promises from transforms? I have a case where I need to perform some async operation in a transform.

@mzabriskie
Copy link
Member

Can you give me an example of what you're trying to do with async transforms?

@kentcdodds
Copy link

I remember once wanting to make a request in a transform to get more info
for my model because the endpoint I was hitting didn't have everything I
needed for my model. $http supports this so that was helpful.

  • Kent C. Dodds
    Sent from my mobile device, please forgive any errors or brevity. (I may
    have used speech to text...)
    On Oct 19, 2014 2:09 PM, "Matt Zabriskie" notifications@github.com wrote:

Can you give me an example of what you're trying to do with async
transforms?


Reply to this email directly or view it on GitHub
#19 (comment).

@nickdima
Copy link
Author

I wanted to convert the data into another format and I was using some async API, but in the end I think doing it outside the request is better in this case.

@mzabriskie
Copy link
Member

@nickdima I think that interceptors are probably what you are looking for. I have supporting interceptors on the road map (see #14).

@kentcdodds
Copy link

I remembered my use case better. We had data that had a slug and we needed to convert that to the actual value for the model, to get the values of the slugs, we needed to make a request for those. There was a race condition where the request for the data came back before the slug values request came back. This is why it was useful to have async transforms. Perhaps this could also be done with an interceptor though...?

@mzabriskie
Copy link
Member

@kentcdodds yes, this is when you would want interceptors. Based on the Angular spec they are designed to handle async operations. Transformers are for mutating request/response data (JSON.stringify, JSON.parse, etc.), and for modifying headers. Interceptors are for any kind of pre processing on requests, and post processing on responses, either synchronously or asynchronously. With an interceptor you have access to the full config for the request, not just data, and headers as with transformers. This allows for global error handling, timing requests, authentication, session restoration, etc. In your case you could asynchronously fetch data from the server before the request was sent, then modify the data being sent in the original request with the response of the nested request.

@kentcdodds
Copy link

Awesome. Come to think of it, we may have switched over to an interceptor to accomplish what we were trying to do. Haha. Thanks for explaining that @mzabriskie!

@mzabriskie
Copy link
Member

This is a duplicate and will be resolved with #14

@samuelcastro
Copy link

Hey @nickdima, I'm in the same situation here trying to parse responses converting String dates into moment.js object, how did you fix that? I've created a interceptor but its taking much time to parse it when we have a big response, did you a find a way to do that asynchronously?

@axios axios locked and limited conversation to collaborators May 21, 2020
simllll pushed a commit to hokify/axios that referenced this issue Oct 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants