Hi all,
I have encountered an issue with the following code that fails to compile:
let message = Result { try decoder.decode(ConsumerMessage.self, from: payload) }
.flatMap { (msg: ConsumerMessage) async in
let analysed = await wa.doesContainViolentOrHarmfulMeaning(keywords: msg.interests)
if (analysed) {
return Result.failure(ConsumerError.harmfulOrViolentInterests("Interests contain harmful or violent meaning"))
} else {
return await redis.sadd(msg.interests, to: RedisKey(id)).get() != 0 ? Result.failure(ConsumerError.storeInterestsFailed("Failed to publish the interests")) : Result.success("Interests have been successfully published")
}
}
The compiler raises the following complaints:
Cannot pass function of type '(ConsumerMessage) async -> Result<_, any Error>' to parameter expecting synchronous function type
Generic parameter 'NewSuccess' could not be inferred
How can I execute asynchronous code within the flatMap
closure? I couldn't find an async variant in the documentation at https://developer.apple.com/documentation/swift/result/flatmap(_:)
.
Best regards