1

I'm trying to do the following, and my question is commented inline. How can I send both the registeredUsersRole and the users from query.find() to the next then in the chain without creating a nested then in there?

// ...
registeredUsersRoleQuery.first({
    useMasterKey: true
}).
then(function(registeredUsersRole) {
    // This means that role was found, so simply return it to the next promise
    return registeredUsersRole;
}, function() {
    // This means that role wasn't found, so create it and return it to the next promise
    var registeredUsersRoleAcl = new Parse.ACL();
    registeredUsersRoleAcl.setPublicReadAccess(false);
    registeredUsersRoleAcl.setPublicWriteAccess(false);
    return new Parse.Role(registeredUsersRoleName, registeredUsersRoleAcl).save{
        useMasterKey: true
    });
}).
then(function(registeredUsersRole) {
    var query = new Parse.Query(Parse.User);
    query.equalTo('verificationCodeVerified', true);

    // How can I send both the registeredUsersRole and the users from 
    // query.find() to the next then in the chain without creating 
    // a nested then in here?
    query.find({
        useMasterKey: true
    }).
    then(function(allVerifiedUsers) {
        registeredUsersRole.getUsers().
        add(allVerifiedUsers);
        return registeredUsersRole.save();
    });
}).
then(function() {
    // How do I have both the registeredUsersRole and the users here?
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
John Doe
  • 1,005
  • 2
  • 8
  • 23
  • There's nothing wrong with creating a nesting closure for this. – Bergi Aug 14 '14 at 19:38
  • Of course, a little less indentation [wouldn't matter](http://stackoverflow.com/posts/25316196/revisions) :-) – Bergi Aug 14 '14 at 19:41
  • @Bergi That was the JS beautifier thing I use with my IDE. I just wanted to catch the error that might come from a previous then along the same chain. An error from above can't be caught in a nested then, right? Anyway, can the nestedness be flattened out with the mentioned context passed along appropriately? – John Doe Aug 14 '14 at 20:22
  • An error from above won't be caught by handlers on the nested then (like `catch`ing exceptions from outside of the `try` block), yes, but all errors (both from above and from nested promises) will be caught by handlers on the outermost promise. – Bergi Aug 14 '14 at 20:27
  • The nestedness can be flattened to some degree by passing along the context, yes, but it's not worth the hazzle for a single level like in your example. – Bergi Aug 14 '14 at 20:28
  • @Bergi I understand for this specific case, but I was looking for a way to do so anyway just for my information for future use. – John Doe Aug 14 '14 at 21:44
  • possible duplicate of [How do I access previous promise results in a .then() chain?](http://stackoverflow.com/questions/28250680/how-do-i-access-previous-promise-results-in-a-then-chain) – Bergi Jan 31 '15 at 10:52

1 Answers1

1

You can use Parse.Promise.when:

registeredUsersRoleQuery.first({ useMasterKey: true }).
then(null , function() {// null ignores
    // ...
    return new Parse.Role(...).save({useMasterKey: true });
}).
then(function(registeredUsersRole) {
    var query = new Parse.Query(Parse.User);
    query.equalTo('verificationCodeVerified', true);
    return Promise.when([
        registeredUsersRole, 
        query.find({ useMasterKey: true })
    ]);
}).
then(function(registeredUsersRole, allVerifiedUsers) {
     // access both here, no nesting was needed
});
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504