Start a new topic

Process an array of promises

I have a case where I need to fetch data from multiple collections, and once all the promises return some response, then we need to loop over the response for all promises as single JSON object and do some further calculations.


I am having a piece of code which is working for HTML SDK -

var promises,

          _this = this;

        promises = _.map(ccus, function(ccu) {

          var query;

          query = new Kinvey.Query().greaterThan('is_occupied', 0).greaterThanOrEqualTo('_id', startDate.toISOString()).lessThanOrEqualTo('_id', endDate.toISOString());

          return Kinvey.Ext.DataStore.findWithFlags("" + ccu.username + "SystemTS60", query, {}, {

            fields: 'comfort_index,comfort_selector,is_occupied'

          });

        });

        return Kinvey.Defer.all(promises).then(function(results) {

          var averageComfort, averageComfortVsEconomy, systemStats, withValidComfortIndex;

          systemStats = _.flatten(results, true);

          withValidComfortIndex = _.filter(systemStats, function(stat) {

            return stat.comfort_index > 0;

          });

          averageComfort = math.average(withValidComfortIndex.map(function(stat) {

            return stat.is_occupied * stat.comfort_index;

          }));

          averageComfortVsEconomy = ((math.average(_.pluck(systemStats, 'comfort_selector').filter(function(num) {

            return num > 0;

          })) - 2) / 3) * 100;

          return _this.trigger('comfortStatsChanged', averageComfort, averageComfortVsEconomy);

        });



Can you please let me know how this can be achieved using the Backbone.js SDK.

Login or Signup to post a comment