1

I'm new to promises and I feel like this should be a pretty simple thing to do.

I would like the below code to stop at the first then if the variable returns true:

           if (groupCodes.length === 1) {
                // Get all does not populate the group object so it will just be the identifier as we require
                var groupIdentifier = groupCodes[0].group;

                GroupMember
                .nestedCreate({ groupId: groupIdentifier, type: groupCodes[0].type }, { })
                .$promise
                .then(function (group) {
                    var isUserAlreadyInGroup = _.includes(group, user._id);
                    if (isUserAlreadyInGroup) {
                        // If this variable returns true I would like to preent the below then function from running
                        notify.info('You have already joined ' + scope.groupCode);
                    }
                })
                .then(function () {
                    // Now that we have joined the group, we have permission
                    // to access the group data.
                    return Group
                        .getById({ groupId: groupIdentifier })
                        .$promise;
                })
           }
István
  • 5,057
  • 10
  • 38
  • 67
Max Lynn
  • 1,738
  • 6
  • 22
  • 35
  • Return a value from the first `then` method. Interrogate that value in the second `then` to determine if you should continue. – Evan Trimboli May 27 '16 at 11:57
  • What are you using for your promises? – forgivenson May 27 '16 at 12:01
  • The question asked is in fact a dupe, but the solution in this scenario is much simpler than complex promise alchemies.... Just **merge** the two functions with an if-else statement – Alex May 27 '16 at 12:17

2 Answers2

1

Depending on your answer to my question, if the promise library you are using doesn't offer a way to do what you want, you can always just have the second then be a separate function that is conditionally called from the first then function.

First, define the second then as a separate function.

var secondThen = function(groupIdentifier) {
  // Now that we have joined the group, we have permission
  // to access the group data.
  return Group
    .getById({
      groupId: groupIdentifier
    })
    .$promise;
};

Here is the then snippet from your example, using my suggestion.

.then(function(group) {
    var isUserAlreadyInGroup = _.includes(group, user._id);
    if (isUserAlreadyInGroup) {
      notify.info('You have already joined ' + scope.groupCode);
    } else {
      // I'm not sure where `groupIdentifier` comes from, I assume you can figure that out, since you had it in your code
      secondThen(groupIdentifier);
    }
})
forgivenson
  • 4,394
  • 2
  • 19
  • 28
0

Try this:

if (groupCodes.length === 1) {
    // Get all does not populate the group object so it will just be the identifier as we require
    var groupIdentifier = groupCodes[0].group;

    GroupMember
     .nestedCreate({ groupId: groupIdentifier, type: groupCodes[0].type }, { })
     .$promise
     .then(function (group) {
         var isUserAlreadyInGroup = _.includes(group, user._id);
         if (isUserAlreadyInGroup) {
             // If this variable returns true I would like to preent the below then function from running
             notify.info('You have already joined ' + scope.groupCode);
         }

         return isUserAlreadyInGroup;
     })
     .then(function (isUserAlreadyInGroup) {
         if (!isUserAlreadyInGroup) {
             // Now that we have joined the group, we have permission
             // to access the group data.
             return Group
              .getById({ groupId: groupIdentifier })
              .$promise;
             });
         })
     }
István
  • 5,057
  • 10
  • 38
  • 67