I have a method that returns a Future:
func getItem(id: String) -> Future<MediaItem, Error> {
return Future { promise in
// alamofire async operation
}
}
I want to use it in another method and covert MediaItem
to NSImage
, which is a synchronous operation. I was hoping to simply do a map
or flatMap
on the original Future but it creates a long Publisher that I cannot erased to Future<NSImage, Error>
.
func getImage(id: String) -> Future<NSImage, Error> {
return getItem(id).map { mediaItem in
// some sync operation to convert mediaItem to NSImage
return convertToNSImage(mediaItem) // this returns NSImage
}
}
I get the following error:
Cannot convert return expression of type 'Publishers.Map<Future<MediaItem, Error>, NSImage>' to return type 'Future<NSImage, Error>'
I tried using flatMap
but with a similar error. I can eraseToAnyPublisher
but I think that hides the fact that getImage(id: String
returns a Future.
I suppose I can wrap the body of getImage
in a future but that doesn't seem as clean as chaining and mapping. Any suggestions would be welcome.