【发布时间】: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