简体   繁体   English

如何阻止多个异步Parse云调用直到完成?

[英]How to block multiple asynchronous Parse cloud calls until done?

I'm trying to achieve something very much like the following within Javascript on a Parse.com Cloud function: 我正在尝试在Parse.com Cloud函数的Javascript中实现类似于以下内容的功能:

Parse.Cloud.define("GatherThings", function(req, res) {
  var array_whatToGet = req.params.getThese;
  var returnVal = {};

  if(array_whatToGet.indexOf("Thing1") > -1) {
    Parse.Cloud.run("GetThing1", {}, {
      success: function(results) {
        returnVal['Thing1'] = results;
      } //ignoring error for now
    }
  }

  if(array_whatToGet.indexOf("Thing2") > -1) {
    Parse.Cloud.run("GetThing2", {}, {
      success: function(results) {
        returnVal['Thing2'] = results;
      } //ignoring error for now
    }
  }

  if(array_whatToGet.indexOf("Thing3") > -1) {
    Parse.Cloud.run("GetThing3", {}, {
      success: function(results) {
        returnVal['Thing3'] = results;
      } //ignoring error for now
    }
  }

  res.success(retVal);
}

Possible syntax errors here aside, when I try to do something like this I can get every called cloud function to run, but because of the async quality of these cloud functions I always get to retVal before anything finishes. 除了可能的语法错误外,当我尝试执行类似的操作时,我可以运行每个被调用的云函数,但是由于这些云函数的异步质量,我总是在任何事情完成之前就进入retVal。

Can I somehow chain or block these in a way that they are not dependent on the other to run, but the final return is dependent on whether or not everything has finished? 我能否以某种方式将它们链接或阻止,使它们不依赖于另一个来运行,但最终的回报取决于一切是否完成?

This is just what promises are for. 这正是诺言的目的。 All of the Parse functions return promises, which can be given a method to run upon completion. 所有的Parse函数都返回诺言,可以为诺言提供一种在完成时运行的方法。 Parse.Promise.when() takes an array of promises and is fulfilled when all of the passed promises are. Parse.Promise.when()接受一个promise数组,并在所有传递的promise被满足时实现。 We'd use it this way: 我们将以这种方式使用它:

// underscore.js is a great general purpose utility, mostly self-explanatory
var _ = require('underscore');

Parse.Cloud.define("GatherThings", function(req, res) {
    var things = ["Thing1", "Thing2", "Thing3"];
    var array_whatToGet = req.params.getThese;
    var promises = [];
    _.each(things, function(thing) {
        if (_.contains(array_whatToGet, thing)) {
            promises.push(getThing(thing));
        }
    });
    Parse.Promise.when(promises).then(function(result) {
        res.success(result);
    }, function(error) {
        res.error(error);
    });
});

The question doesn't mention what GetThing1, GetThing2 and GetThing3 are, but it implies that they are other cloud functions. 这个问题没有提到GetThing1,GetThing2和GetThing3是什么,但这意味着它们是其他云函数。 Calling Cloud.run from the cloud is at best unnecessary. 最好不要从云中调用Cloud.run。 Lets assume that the things specify some object to be queried and make a regular, non-cloud function that returns a promise to perform a query (which the Query methods all do). 让我们假设事物指定了要查询的对象,并创建了一个常规的非云函数,该函数返回一个执行查询的承诺(Query方法都可以这样做)。

function getThing(aThing) {
    var query = new Parse.Query("MyClass");
    query.equalTo("theThingProperty", aThing);
    // query.first() returns a promise that is fulfilled when the query is complete
    return query.first();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何阻止执行直到执行异步调用? - how to block execution until Asynchronous call is executed? 如何知道页面上的所有异步调用何时完成(Parse Cloud函数)? - How can I tell when all of the asynchronous calls on my page have finished (Parse Cloud functions)? 如何在执行多个异步调用时阻止调用函数,然后继续处理收集的数据? - How to block calling function while multiple asynchronous calls are executed, then contiune processing the collected data? “ it”块执行多次,直到在Jasmine 2.0中调用“ done”为止 - 'it' block executes multiple times until 'done' is called in Jasmine 2.0 如何与ajax调用异步运行并完成显示加载程序? - How to function with ajax calls asynchronously and display loader until done? 如何等到多个 ajax 请求完成? - How to wait until multiple ajax requests are done? nodejs阻塞,直到“ for”迭代完成 - nodejs block until 'for' iteration done 如何强制javascript等待/阻止直到完成延迟的工作? - How to force javascript wait/block until deferred jobs done? 如何在JavaScript中清晰地构造多个异步调用? - How to cleanly structure multiple asynchronous calls in javascript? 如何返回多个异步调用的响应? - How to return the response of multiple asynchronous calls?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM