Skip to content

Instantly share code, notes, and snippets.

Promise() { fulfill, reject in
// code
}
Promise() { fulfill, reject in
return "Hello World" //or other async code
}
fetchPromise().then { value in
// do something
}.catch { error in {
// in case of error
}
fetchPromise().then { value in
return fetchPromise2(value)
}.then { value in
// do something
}.catch { error in
// error
}
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
}
use_frameworks!
target "target" do
pod "PromiseKit", "~> 6.0"
end
github "mxcl/PromiseKit" ~> 6.0
package.dependencies.append(
.Package(url: "https://github.com/mxcl/PromiseKit", majorVersion: 6)
)
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
Guarantee { seal in
seal("Hello World")
}