我有一个基于 Vapor 应用程序的非常简单的服务。该服务使用来自另一个服务系列的数据。显然,这只是这些map
方法所针对的应用程序。
链中的所有回调都执行,但链中的最后一个EventLoopFuture
从未完成,导致我的服务无限期挂起。
此代码进行一次异步调用以获取会话 ID,然后使用该数据检查一个人是否是特定组的成员。对其他服务的调用返回合理的值 - 目前第二次调用总是返回错误。
当我调用此方法时,所有回调都会按预期执行和运行。
请注意,在此代码片段中,我已经“解开”了序列的各个阶段,以尝试找出问题所在,但整体行为不受影响。
我已经尝试了许多不同的链排列,使用flatMap
,map
和flatMapError
在适当的情况下,而不改变最终结果。
let authService = remoteApi.authServices();
// EventLoopFuture<String> with sessionId
let sessionIdFuture = authService.getSessionId(username: userName, password: password)
// EventLoopFuture<Bool> with whether the user is in a particular group
let gsFuture = sessionIdFuture.flatMap { sessionId -> EventLoopFuture<Bool> in
let groupMemberService = remoteApi.groupMemberServices()
return groupMemberService.personIsInGroup(sessionId: sessionId, groupId: groupId, userId: userId)
}
// EventLoopFuture<Bool> if the above has an error, just return false
let errorFuture = gsFuture.flatMapErrorThrowing { error in
// This executes successfully!
return false
}
// EventLoopFuture<String> with the return data
let returnFuture = errorFuture.flatMapThrowing { isMember -> String in
// This executes successfully!
let response = PersonIsMemberResponse(isMember: isMember)
if let json = self.encodeResponse(response) {
print(json) // {"isMember": false}
return json
} else {
throw Abort(.internalServerError, reason: "could not encode our own dang data type");
}
}.always { result in
// This executes!
do {
try remoteApi.shutdown()
} catch {
print(error)
}
}
gsFuture.whenComplete { result in
// This executes!
print("gsFuture complete!")
}
errorFuture.whenComplete { result in
// This executes!
print("errorFuture complete!")
}
returnFuture.whenComplete { result in
// This DOES NOT execute!
print("returnFuture complete!")
}
我看不到最后一个如何flatMapThrowing
执行并返回一个值,那么未来就不完整。我错过了什么?