从Parse承诺链返回数据 [英] Returning data from Parse promise chain

查看:56
本文介绍了从Parse承诺链返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我已经了解了Parse承诺链,但我不明白的是我如何从函数中返回我的数据(i)备份promise链和(ii)回到我的调用方法原始JS代码。



使用下面的代码,调用第一个Parse查询,然后调用a(),最后调用b(),这一切都很好。每个阶段的console.log都用于测试目的,并显示链已经按顺序执行。



我现在有3个问题:


  1. 如何从函数a()和函数b()中获取数据,以便我可以在main函数getUserCompetitionTokens()中访问它? / p>


  2. 如何将getUserCompetitionTokens()中的任何数据返回到调用此Parse代码的主程序?


  3. 如果我想将函数a()以及函数b()中的数据返回到我的主程序怎么办?







  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()。然后(a).then(b);

函数a(user_comp_results){

var no_results = user_comp_results.length;
var id = user_comp_results [0] .id;
console.log(用户竞争输出:+ no_results +找到结果,第一项ID:+ id);

var Competition = Parse.Object.extend(Competition);
var query = new Parse.Query(Competition);
返回query.get(comp_id,{
成功:函数(竞争){
console.log(COMP - + competition.id);
},
错误:函数(竞争,错误){
console.log(错误);
}
});
}

函数b(竞争){

var Competition = Parse.Object.extend(Competition);
var query = new Parse.Query(Competition);
query.get(comp_id,{
成功:函数(竞争){
console.log(COMP 2 - + competition.id);
},
错误:函数(竞争,错误){console.log(错误);}
});
}
}


解决方案

不要 返回:)



对不起,这只是技术性的一个笑话:你请参阅,Promises是一种表达异步行为的方式。所以你不能严格地说,抓住函数的 return 值。



但是,你已经抓住了每一步的结果正确...你只是没有意识到你可以自己使用它。



答案是使用你自己的然后 handler。

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

但请记住,承诺可能失败。这就是为什么传递第二个处理程序很重要的原因,只要有错误就会调用它。

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


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.

Using the code below, the first Parse query is called, then a() and finally b() which is all fine. The console.log at each stage are for test purposes and show that the chains have been executed and in order.

I now have 3 questions:

  1. How do I get the data back from function a() and function b() so I can access it in the main function getUserCompetitionTokens()?

  2. How do I return any data from getUserCompetitionTokens() to the main program which has called this Parse code?

  3. What if I want the data from function a() and also from function b() to BOTH be returned to my main program?


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. 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.

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

However, keep in mind that a Promise might fail. 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);

这篇关于从Parse承诺链返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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