简体   繁体   English

从Parse承诺链返回数据

[英]Returning data from Parse promise chain

I think I have got my head around Parse promise chains, but what I don't understand is how I return my data from the functions (i) back up the promise chain and (ii) back to the calling method of my original JS code. 我想我已经掌握了Parse承诺链,但我不明白的是我如何从函数返回我的数据(i)备份promise链和(ii)返回原始JS代码的调用方法。

Using the code below, the first Parse query is called, then a() and finally b() which is all fine. 使用下面的代码,调用第一个Parse查询,然后是a(),最后是b(),这一切都很好。 The console.log at each stage are for test purposes and show that the chains have been executed and in order. 每个阶段的console.log都用于测试目的,并显示链已按顺序执行。

I now have 3 questions: 我现在有3个问题:

  1. How do I get the data back from function a() and function b() so I can access it in the main function getUserCompetitionTokens()? 如何从函数a()和函数b()中获取数据,以便我可以在main函数getUserCompetitionTokens()中访问它?

  2. How do I return any data from getUserCompetitionTokens() to the main program which has called this Parse code? 如何将getUserCompetitionTokens()中的任何数据返回到调用此Parse代码的主程序?

  3. What if I want the data from function a() and also from function b() to BOTH be returned to my main program? 如果我想从函数a()以及从函数b()到BOTH的数据返回到我的主程序怎么办?


function getUserCompetitionTokens(comp_id) {
    Parse.initialize("****","****");
    currentUser = Parse.User.current();

    user_competition = Parse.Object.extend("UserCompetition");
    var user_comp_query = new Parse.Query(user_competition);
    user_comp_query.equalTo("UserParent", currentUser);
    user_comp_query.find().then(a).then(b);

    function a(user_comp_results) {

        var no_results = user_comp_results.length;
        var id = user_comp_results[0].id;
        console.log("User Competition Output: " + no_results + " results found, first item id: " + id);

        var Competition = Parse.Object.extend("Competition");
        var query = new Parse.Query(Competition);
        return query.get(comp_id, {
            success: function(competition) {
                console.log("COMP - " + competition.id);
            },
            error: function(competition, error) {
                console.log(error);
            }
        });
    }

    function b(competition) {

        var Competition = Parse.Object.extend("Competition");
        var query = new Parse.Query(Competition);
        query.get(comp_id, {
            success: function(competition) {
                console.log("COMP 2 - " + competition.id);
            },
                error: function(competition, error) {console.log(error);}
        });
    }
}

You don't return :) 回来 :)

I'm sorry, that is just a joke on a technicality: you see, Promises are a way to express asynchronous behaviour. 对不起,这只是技术性的一个笑话:你看,Promises是表达异步行为的一种方式。 So you can't, strictly saying, grab the return value of a function. 所以你不能严格地说,抓住函数的返回值。

However, you are already grabbing the results of each step correctly... You just didn't realize you can use it yourself. 但是,您已经正确地抓住每个步骤的结果......您只是没有意识到您可以自己使用它。

The answer is to use your own then handler. 答案就是使用自己then处理。

user_comp_query.find().then(a).then(b).then(function(results){
  console.log(results); // Hooray!
});

However, keep in mind that a Promise might fail. 但是,请记住,Promise 可能会失败。 That's why it's important to pass a second handler, which will be called whenever there is an error. 这就是为什么传递第二个处理程序很重要的原因,只要有错误就会调用它。

var handleSuccess = function (results) {};
var handleFailure = function (error) {};
var parsePromise = user_comp_query.find().then(a).then(b);
parsePromise.then(handleSuccess, handleFailure);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM