2

I have an array of Promise of different type. In this particular case Promise<URL> and Promise<UIImage> which I want to provide to when(fulfilled:)

As an example:

let fileName = "somefilename"

 let videos: [URL] = selectedItems.compactMap { guard $0.isVideo else {return nil}; return URL(string: $0.url) }
 let images: [URL] = selectedItems.compactMap { guard !$0.isVideo else {return nil}; return URL(string: $0.url) }
 let videoRequests = videos.map { RemoteDownloadManager.shared.downloadVideo(from: $0, withName: fileName) }
 let imgRequests = images.map {RemoteDownloadManager.shared.downloadImage(from: $0)}

I have two arrays of promises, but of different types: [Promise<UIImage>] and [Promise<URL].

Is there a way to call when(fulfilled) with a combined, casted, array?

when(fulfilled: [imgRequests, videoRequests])
        .done { items in
                  // [...]
}

This won't compile (Obviously) with error: Cannot invoke 'when' with an argument list of type '(fulfilled: [Any])'

Ideas?

superandrew
  • 1,741
  • 19
  • 35

1 Answers1

0

You should be able to achieve this by cast you array like this:

let promiseArray = [promise1, promise2] as! [Promise<Any>]
when(fulfilled: promiseArray).done { items in
                  // [...]
}
Skodik.o
  • 506
  • 4
  • 21