Skip to content

Instantly share code, notes, and snippets.

@olbrichj
Created March 21, 2018 11:26
Show Gist options
  • Save olbrichj/cf0414a47bbd02276fe37701e69fd0f0 to your computer and use it in GitHub Desktop.
Save olbrichj/cf0414a47bbd02276fe37701e69fd0f0 to your computer and use it in GitHub Desktop.
func fetch(url: URL) -> Promise<Data> {
return Promise { seal in
URLSession.shared.dataTask(with: url!) { data, _, error in
seal.resolve(data, error)
}.resume()
}
}
firstly {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
return fetch(url: <backendURL>)
}.then { data in
return JSONParsePromise(data) // we skip the wrapping of JSONParsing
}.then { imageurl in
return fetch(url: imageurl)
}.then { data in
imageView.image = UIImage(data: data)
}.ensure {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}.catch { error in
// in case we have an error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment