本文介绍了如何将额外的数据向下传递到 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 以便它们并行执行,则不能使用父作用域技巧(想象一下围绕上述语句的 for 循环,所有承诺被放在一个数组中,然后使用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.

推荐答案

您可以使用 闭包 来做到这一点,不需要任何额外的对象或包装.

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.

根据评论编辑

如果您的 Promise 链足够长且足够复杂,以至于这种嵌套语法变得笨拙,那么逻辑可能足够复杂,值得将其抽象为自己的函数.

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 链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:46