如何在Parse Promise链中传递额外数据 [英] How to pass extra data down a Parse Promise chain

查看:68
本文介绍了如何在Parse Promise链中传递额外数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Parse Cloude代码中,我需要运行几个连续的查询,每个查询使用find()。

In my Parse Cloude code I need to run a few successive queries, each of them using a "find()".

示例:

var promise = firstQuery.get(objectId).then(function(result1){
            return secondQuery.find();
        }).then(function(result2){
            return thirdQuery.find();
        }).then(function(result3) {

             // here I want to use "result1", "result2" and "result3"
        });

问题是:如何在最后then中访问result1和result2语句,而不将它们分配给父作用域中声明的变量。

The question is: how do I access "result1" and "result2" in the final "then" statement, without assigning them to variables declared in the parent scope.

为什么我这样问:如果要嵌套在循环中创建的一堆promise,以便在它们中执行,则不能使用父作用域技巧parallel(假设围绕上述语句的for循环,其中所有promises都放在一个数组中,然后使用Parse.Promise.when进行评估。它们都将同时开始修改父作用域变量。)

Why do I ask this: You cannot use the parent scope trick if you are nesting a bunch of promises which you create in a loop in order for them to be executed in parallel (imagine a for loop around the above statement whereby all the promises are put in an array and then evaluated using "Parse.Promise.when". They would all start modifying the parent scope variables at the same time.)

我可以创建某种承诺对象,我可以返回以下内容:

Can I create some kind of promise object where I could return something along the lines of:

Parse.promise({result:result1,findResult:secondQuery.find()};

所以我可以通过

result2.result 

result2.findResult

我希望自己清楚。这不是很容易解释。

I hope I make myself clear. This is not very easy to explain.

推荐答案

你可以使用< a href =https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures =noreferrer>闭包这样做,无需任何额外的对象或者包装。

You can make use of closures to do this, without the need for any extra objects or wrapping.

var promise = firstQuery.get(objectId).then(function(result1){
    return secondQuery.find()
    .then(function(result2) {
        return thirdQuery.find()
        .then(function(result3) {
            //can use result1, result2, result3 here
        });
    });
});

这种嵌套语法在功能上与你的链接语法相同。

This "nested" syntax is identical in function to your "chaining" syntax.

根据评论进行编辑

如果您的承诺链冗长而复杂,这种嵌套语法变得笨拙,逻辑可能足够复杂,值得抽象到自己的函数中。

If your promise chain is long and complex enough that this nested syntax becomes ungainly, the logic is probably complex enough to merit abstraction into its own function.

function complexQuery(objectId) {
    var results = {};
    return firstQuery.get(objectId).then(function(result1) {
        results.result1 = result1;
        return secondQuery.find();
    })
    .then(function(result2) {
        results.result2 = result2;
        return thirdQuery.find();
    })
    .then(function(result3) {
        results.result3 = result3;
        return results;
    });
}

complexQuery(objectId)
.then(function (results) {
    //can use results.result1, results.result2, results.result3
});

就我个人而言,我认为这比使用更容易阅读和维护。 bind

Personally, I think that's easier to read and maintain than messing around with .bind.

这篇关于如何在Parse Promise链中传递额外数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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