javascript - wait for query to finish before sending response -


i'm using cloud code execute on server side. in code had series of queries should in while loop. queries should done after each other. when run code, goes each query , returns before finishing queries, hence getting socket timeout connection error. there way wait query finish before proceeding? example highly appreciated. thank you.

edit: here code snippet

while (i < cities.length-1){             if (query) {                 query = false;                 cities.foreach(function(object){                     var query = new parse.query("pictures");                     query.descending("likes");                     query.equalto("city", object);                     query.limit(1);                     query.find().then(function(results){                         success: function(results) {                             var temparray = new array();                             temparray = results;                              rankedposts = rankedposts.concat(temparray);                              query = true;                             i++;                         }, error: function() {                             response.error("error");                         }                     });                 });             };         } 

i think need make promise series

see https://parse.com/docs/js_guide#promises-series

make this:

  // create trivial resolved promise base case.   var promise = parse.promise.as();    var finalresults = [];    // objects in array...   _.each(cities, function(objectx) { // "_" given using "var _ = require('underscore');" @ beginning of module         // each item, extend promise function query specified objectx        promise = promise.then(function() {             var subpromise = new parse.promise();             var query = new parse.query("pictures");            query.descending("likes");            query.equalto("city", objectx);            query.limit(1);             query.find().then(function(results) {                 // append cur results final results                finalresults = _.union (finalresults,results);                subpromise.resolve(results);             }, function(error) {                 subpromise.reject(error);             });             return subpromise;        });    });   return promise;  }).then(function() {    // when queries have been performed  }); 

i've not tested code, i've used success.

anyway, keep in mind restriction of parse.com request duration. so, 3 seconds in events listener (like beforesave or aftersave), 7 seconds in custom functions , 15 minutes maximum in background jobs.

hope helps


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -