I want to upload multiple images asynchronously.
how can I write it using PromiseKit?
let images:[UIImage]!
self.upload(image0).then {_ in
self.upload(image1)
}.then {_ in
self.upload(image2)
}.then {_ in
self.upload(image3)
}
....
.then {image9 in
self.upload(image9)
}
or
for image in images {
....
}
when(fulfilled: images.map(upload)).then { results in
//…
}
Or if for some reason you really want to do them serially:
var images = images
return Promise(value: ()).then { _ -> Promise<Void> in
if let img = images.popFirst {
return self.upload(img)
} else {
return Promise(value: ())
}
}
However you should have good reason to not use when because it will be n times faster.
@mxcl Thank you,I really appreciate.
Most helpful comment
Or if for some reason you really want to do them serially:
However you should have good reason to not use
whenbecause it will bentimes faster.