Skip to content

Instantly share code, notes, and snippets.

fetch(url: <imageurl>).recover { error -> Promise<UIImage> in
return UIImage(name: <placeholder>)
}...
fetch(url: <backend-url>).then { value in
// value
}
func fetch(url: URL) -> Promise<Data> {
return Promise { seal in
URLSession.shared.dataTask(with: url!) { data, _, error in
seal.resolve(data, error)
}.resume()
}
}
Guarantee { seal in
seal("Hello World")
}
func fetch(url: URL) -> Promise<Data> {
return Promise { seal in
URLSession.shared.dataTask(with: url!) { data, _, error in
seal.resolve(data, error)
}.resume()
}
}
fetch(url: <backendURL>).then { data in
return JSONParsePromise(data) // we skip the wrapping of JSONParsing
package.dependencies.append(
.Package(url: "https://github.com/mxcl/PromiseKit", majorVersion: 6)
)
github "mxcl/PromiseKit" ~> 6.0
use_frameworks!
target "target" do
pod "PromiseKit", "~> 6.0"
end
fetchPromise().then { value in
return errorPromise(value) // this will throw an error
}.then { value in
//this will not execute on error
}.catch { error in
//we got an error
}
fetchPromise().then { value in
return fetchPromise2(value)
}.then { value in
// do something
}.catch { error in
// error
}