等待查询完成后再发送响应 [英] wait for query to finish before sending response

查看:168
本文介绍了等待查询完成后再发送响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cloud Code在服务器端执行某些操作.在我的代码中,我不得不执行一系列查询,这些查询应该在while循环中执行.这些查询应该互相进行.当我运行代码时,它会进入每个查询并在完成查询之前返回,因此会收到套接字超时连接错误.有没有办法在继续进行之前等待查询完成?任何例子将不胜感激.谢谢.

i'm using Cloud Code to execute something on the server side. In my code i had to do a series of queries which should do in a while loop. Those queries should be done after each other. When i run the code, it goes into each query and returns before finishing the queries, hence getting a socket timeout connection error. Is there a way to wait for a query to finish before proceeding? any example will be highly appreciated. Thank you.

这是一个代码段

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");
                        }
                    });
                });
            };
        }

推荐答案

我认为您需要制作一个 Promise系列

I think you need to make a Promise series

请参见 https://parse.com/docs/js_guide#promises-series

进行如下操作:

  // Create a trivial resolved promise as a base case.
  var promise = Parse.Promise.as();

  var finalResults = [];

  // for all the objects in the array...
  _.each(cities, function(objectX) { // the "_" is given by using "var _ = require('underscore');" at the beginning of your module

       // For each item, extend the promise with a function to 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 to final results
               finalResults = _.union (finalResults,results);
               subPromise.resolve(results);

           }, function(error) {

               subPromise.reject(error);

           });

           return subPromise;
       });

  });
  return promise;

}).then(function() {

  // When all queries have been performed

});

我没有测试过这段代码,但是我已经成功使用了类似的代码.

I've not tested this code, but i've already used something like this with success.

无论如何,请记住Parse.com对请求持续时间的限制. 因此,事件监听器中的时间为3秒(例如beforeSave或afterSave),事件监听器中的时间为7秒 自定义功能,并且在后台作业中最多15分钟.

Anyway, keep in mind the restriction of Parse.com about the request duration. So, 3 seconds in the events listener (like beforeSave or afterSave), 7 seconds in the custom functions and 15 minutes maximum in the background jobs.

希望有帮助

这篇关于等待查询完成后再发送响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆