从Parse承诺链返回数据(Returning data from Parse promise chain)

编程入门 行业动态 更新时间:2024-10-28 07:22:25
从Parse承诺链返回数据(Returning data from Parse promise chain)

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

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

我现在有3个问题:

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

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

如果我想从函数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);} }); } }

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:

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

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

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);} }); } }

最满意答案

回来 :)

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

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

答案是使用你自己的处理程序。

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

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

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

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);

更多推荐

本文发布于:2023-08-01 03:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352772.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Returning   Parse   chain   promise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!