解析云code:如何调用承诺为节省异步 [英] Parse Cloud Code: How to Invoke Promises for Async Saving

查看:126
本文介绍了解析云code:如何调用承诺为节省异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新生的codeR这是新的异步问题。为简单起见,我创建了一个简单的例子来说明我的问题。我有一个运行一个查询,并用结果,称2其他云功能的简单函数。然后,它会尝试.SET值返回的对象和.save()它。一切似乎正确执行,除.save()。我使用的承诺/ .thens,和一些其他技巧尝试过,但没有奏效。如果有人能提供最简单直接的解决方案,我真的AP preciate它。

I'm a nascent coder that's new to async issues. For simplicity's sake, I've created a quick example to illustrate my problem. I have a simple function that runs a query and with the result, calls 2 other cloud functions. It then attempts to .set values for the returned object and .save() it. Everything seems to execute correctly, except the .save(). I've tried using promises/.thens, and a few other tricks, but nothing has worked. If someone could provide the most simple and direct solution, I would really appreciate it.

Parse.Cloud.define("test", function(request,response){  
    query.equalTo("name",request.params.name);
    query.first(
        success: function(result){
            result.set("testAverage", Parse.Cloud.run("calcAverage",{"name":request.params.name,"type":"test"}));
            result.set("quizAverage", Parse.Cloud.run("calcAverage",{"name":request.params.name,"type":"quiz"}));
            result.save();
        },
        error: function(){
            response.error("error");
        }
    );
    return result;
}); 

P.S。在我的实际情况,有10-20之间并联调用其它云功能,而不仅仅是2。

P.S. in my actual scenario, there are between 10-20 parallel calls to other cloud functions, not just 2.

谢谢!

推荐答案

我相信与您正在编码的事情了result.save()可以在你的云功能,能够返回调用方式。我也不能肯定你的成功回调函数中引用时request.params.name将正常工作。

I believe with the way you are currently coding things that the result.save() may be called before your cloud functions are able to return. I'm also not sure that request.params.name will work properly when referenced inside of your success callback functions.

请确保您的解析版本支持的承诺,然后尝试举办code是这样的:

Make sure your version of parse supports promises, and then try organizing your code like this:

function queryTestAverage(name) {
    return Parse.Cloud.run("calcAverage",{"name":request.params.name,"type":"test"});
}

Parse.cloud.define("test", function(request, response) {

    query.equalTo("name", request.params.name);
    return query.first()
})
.then(function(results) {
    var name = results.get("name");
    Parse.Promise.when([queryTestAverage(name), queryQuizAverage(name)]).then(
        function(result1, result2) {
            results.set("testAverage", result1);
            results.set("quizAverage", result2);
            results.save();
        }

}

注意:以上code是样品,可能无法正常工作,是

Note: Above code is for sample and may not work as is.

信息的来源:

  • General promises: http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/
  • Use of .when() for multiple promises at once: https://www.parse.com/questions/parsepromisewhen-doesnt-work

这篇关于解析云code:如何调用承诺为节省异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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