【问题标题】:How do you stop a promise then function from going onto the next then function? [duplicate]你如何阻止一个 promise then 函数进入下一个 then 函数? [复制]
【发布时间】:2016-09-25 17:58:05
【问题描述】:

我是 Promise 的新手,我觉得这应该是一件非常简单的事情。

如果变量返回 true,我希望下面的代码在第一个 then 处停止:

           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;
                })
           }

【问题讨论】:

  • 从第一个 then 方法返回一个值。在第二个then 中询问该值以确定您是否应该继续。
  • 你用什么来兑现承诺?
  • 问的问题实际上是一个骗子,但这种情况下的解决方案比复杂的承诺炼金术要简单得多......只需将两个函数合并 if- else 语句

标签: javascript


【解决方案1】:

试试这个:

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;
             });
         })
     }

【讨论】:

    【解决方案2】:

    根据您对我的问题的回答,如果您使用的 Promise 库无法提供您想要的方式,您始终可以让第二个 then 成为一个单独的函数,有条件地从第一个then 函数。

    首先,将第二个then 定义为单独的函数。

    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;
    };
    

    这是您示例中的then sn-p,使用我的建议。

    .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);
        }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 2018-11-14
      • 2018-06-12
      • 2020-02-23
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多