0

The following Cloud Code function on Parse Server does not work as expected.

It seems to resolve too early so that not all chained promises of promise2 are executed. In particular I see some of the resultA.add(...); not being executed.

I have tried to re-arrange this several times without success.

I am not sure if return Parse.Promise.resolve(); is what I should return in the else clause or if it is something else or nothing.

Can anyone spot something here?

Parse.Cloud.define("aFunction", function(request, response) {

    var q = new Parse.Query("ClassA");    
    q.find()
    .then(
        function(resultsA) {

            var promise = new Parse.Promise.as();
            resultsA.forEach(function(resultA) {

                promise = promise
                .then(
                    function() {
                        var q = new Parse.Query("ClassB");
                        return q.first();
                    }
                )
                .then(
                    function(resultB) {

                        if (resultB != undefined) {
                            resultA.set(...);
                            return resultA.save();

                        } else {
                            resultA.set(...);
                            return resultA.save();
                        }
                    }
                )
                .then(
                    function() {

                        var q = new Parse.Query("ClassC");
                        return q.find()

                        .then(
                            function(resultsC) {
                                if (resultsC != undefined && resultsC.length > 0) {

                                    var promise2 = new Parse.Promise.as();
                                    resultsC.forEach(function(resultC) {
                                        promise2 = promise2
                                        .then(
                                            function() {
                                                resultA.add(...);
                                                return resultA.save();
                                            }
                                        );
                                    });
                                    return promise2;

                                } else {
                                    return Parse.Promise.resolve();
                                } 
                            }
                        );
                    }
                );
            });
            return promise;
        }
    )
    .then(
        function(result) {
            response.success("success");
        },
        function(error) {
            response.error(error.message);
        }
    );
});

Long code but I already stripped it down and wanted to show the actual structure. Thanks.

Manuel
  • 14,274
  • 6
  • 57
  • 130
  • Yes, `return undefined` or `return Promise.resolve(undefined)` (to the same effect) seem appropriate. – Bergi Jun 23 '16 at 18:24
  • Oh wait, for the Parse API it's `return Parse.Promise.as(undefined)`, not `.resolve` – Bergi Jun 23 '16 at 18:28
  • @Bergi The `.as` method seems to be used to predefine the return value of a resolved promise at the time of promise declaration. To actually resolve the promise it is still necessary to call `.resolve` on it. In other words `Parse.Promise.as(aValue); Parse.Promie.resolve();` is equivalent to `Parse.Promise.resolve(aValue);`. – Manuel Jun 23 '16 at 23:53
  • Oh my, the Parse terminology is so confusing (it doesn't really comply with [any standards](http://stackoverflow.com/q/29268569/1048572)). I always have to double-check [the source](https://github.com/ParsePlatform/Parse-SDK-JS/blob/v1.8.5/src/ParsePromise.js) to be sure. Their `Promise.resolve(x)` and `Promise.as(x)` methods do mostly the same, except that `resolve` treats thenables as expected and `as` just fulfills with the value. In any case, they are exchangeable when used with `undefined` like in your case (and you better should only use one of them for consistency). – Bergi Jun 24 '16 at 01:02

1 Answers1

0

just checked your code. And i'm not sure if it's your type mistake , when you 'clean your code' for showing structure.

in your loop: chats.forEach(function(resultA) ..

It seems like you haven't declare 'chats' variable.

Also, as Parse official recommended , please try following:

var _ = require('underscore.js');

var promise = Parse.Promise.as();
_.each(resultsA, function(resultA) {

    promise = promise.then(function() {..}.then..then..
}
return promise;

Hope this can help you.

Zuyin XU
  • 43
  • 1
  • 5
  • Thanks, `chats` was a typo, code corrected. I am using `Parse.Promises` in other functions and they work fine. I think it is due to the structure of the nested promises that it doesn't work. – Manuel Jun 23 '16 at 18:28