本文介绍了Parse.com:query.each 并行执行,达到限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个批处理作业,我希望它们为每个查询结果连续运行问题是似乎 query.each 并行执行所有内容

I am running a batch job and I would like them to be run serially for each query resultThe problem is that it seems that query.each executes everything in parrallel

如何更改我的代码以使所有内容都串行执行?

How can I change my code to have everything execute serially?

Parse.Cloud.job("fetchMenus", function(request, status) {

  var counter = 0;
  // Query for all users
  var LocationSP = Parse.Object.extend("Location");
  var query = new Parse.Query(LocationSP);
  query.doesNotExist("menu");
  query.equalTo("city_lc","new york");
  // query.limit(10);
  var p = Parse.Promise.as("");

  query.each(function(location) {

    p = p.then(function(){

    if (counter % 100 === 0) {
        // Set the  job's progress status
        status.message(counter + " users processed.");
      }
    // console.log(location);
    // console.log(location.get("location_id"));

        Parse.Cloud.run('getMenu2', {alias: location.get("location_id") }, {
          success: function(result) {
            // result is 'Hello world!'
            counter += 1;
            return Parse.Promise.as("1");
          },
          error: function(error) {
            return Parse.Promise.as("1");
          }
        });
    })

    return p;


  }).then(function() {
    // Set the job's success status
    status.success("Migration completed successfully.");
  }, function(error) {
    // Set the job's error status
    status.error("Uh oh, something went wrong." + error);
  });
});

推荐答案

query.eachParse.Cloud.run 都返回一个 promise,所以你可以简单地写如下:

Both query.each and Parse.Cloud.run return a promise, so you can simply write as following:

Parse.Cloud.job("fetchMenus", function(request, status) {
  var LocationSP, counter, query;
  counter = 0;
  LocationSP = Parse.Object.extend("Location");
  query = new Parse.Query(LocationSP);
  query.doesNotExist("menu");
  query.equalTo("city_lc", "new york");
  return query.each(function(location) {
    if (counter % 100 === 0) {
      status.message(counter + " users processed.");
    }
    return Parse.Cloud.run('getMenu2', {
      alias: location.get("location_id")
    }).then(function(result) {
      counter += 1;
      return Parse.Promise.as("1");
    }, function(error) {
      return Parse.Promise.as("1");
    });
  }).then(function() {
    return status.success("Migration completed successfully.");
  }, function(error) {
    return status.error("Uh oh, something went wrong." + error);
  });
});

这篇关于Parse.com:query.each 并行执行,达到限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:44