Promise pattern and handling multiple parallel fetches.
D
Daniel
started a topic
almost 7 years ago
I need to fetch data from multiple collections and I want to do this in parallel.
(I am not fetching relational data due to it's limitation (http://devcenter.kinvey.com/html5/guides/datastore#limitations), instead I am fetching collections separately and then I am handling the relations in my front end script. That is why I need to fully fetch all my collections before I proceed).
What is the right way of handling parallel requests? Does the Promise pattern offer any advantage here? To be honest I don't fully understand Promises. Or is there a way to load multiple collections within one fetch request without the limitations?
Hey Daniel, I tagged this under JavaScript so we get the right eyes on it.
M
Mark
said
almost 7 years ago
Yes, you can parallelize fetching data, see below. This executes two network requests concurrently.
```
Kinvey.Defer.all([
Kinvey.DataStore.find('foo-collection'),
Kinvey.DataStore.find('bar-collection')
]).then(function(response) {
var fooResult = response[0];
var barResult = response[1];
}, function(error) {
// either foo or bar failed.
});
```
D
Daniel
said
almost 7 years ago
Hi Mark
this is surely the answer to my problem, you should think about updating the guide on data fetching (http://devcenter.kinvey.com/html5/guides/datastore) to contain this information.
Also does the defer method load all the data in one request or is it just front end handling of multiple asynchronous requests?
Thanks
D
Daniel
said
almost 7 years ago
The documentation is surprisingly unclear :( http://devcenter.kinvey.com/html5/reference/api/Kinvey.Defer.html
M
Mark
said
almost 7 years ago
This will still issue two (concurrent) network requests. If you want to fetch all data in one go, I recommend using a Custom Endpoint instead.
Daniel
(I am not fetching relational data due to it's limitation (http://devcenter.kinvey.com/html5/guides/datastore#limitations), instead I am fetching collections separately and then I am handling the relations in my front end script. That is why I need to fully fetch all my collections before I proceed).
What is the right way of handling parallel requests? Does the Promise pattern offer any advantage here? To be honest I don't fully understand Promises. Or is there a way to load multiple collections within one fetch request without the limitations?
Thanks.