0

I have a few components in one page.

Each of them fetches the same data from the server.

As a result, when the page loads, these components send the same request multiple times.

Is there any way to prevent this? Like caching the promise of the first request and returning that to the next coming requests (before the promise resolved)?

2 Answers 2

1

In order to make sure that the request is sent only once, you can keep track of the first HttpPromise you create, and on subsequent calls of the function, return that same promise.

This SO link might be what you're looking for.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. What if I use $resource instead of $http?
@ThịnhPhạm $resource also uses $http internally, I think it should work fine.
0

When calling the $http service you can additionally supply a cache object. If you do so any additional requests will use the cached value. If the same cache is used then additional requests made before the first is resolved will not call the server but wait for the response.

$http.get(url, {cache:cacheObj})

Where cacheObj is from $cacheFactory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.