Skip to content

Instantly share code, notes, and snippets.

@y-takagi
Last active January 7, 2017 20:37
Show Gist options
  • Save y-takagi/ee5d8a4265e29a84654bdf25bc8c2eb4 to your computer and use it in GitHub Desktop.
Save y-takagi/ee5d8a4265e29a84654bdf25bc8c2eb4 to your computer and use it in GitHub Desktop.
PromiseKit Guide

PromiseKit Guide (v4.1.0)

PromiseKit の使い方。

Create Promise

enum SampleError: Error {
 case error
}

_ = Promise { fulfill, reject in
  if true {
    fulfill()
  } else {
    reject(SampleError.error)
  }
}

// fulfill promise
_ = Promise(value: ())


// reject promise
_ = Promise(error: SampleError.error)

Chain Promise

enum SampleError: Error {
  case error, unexpected(String)
}

firstly {
  Promise(value: "Hello")
}.then { value in
  Promise(value: value + ", World")
}.then { value in
  print(value) // => Hello, World
  return Promise(error: SampleError.unexpected("Error Happened"))
}.always {
  print("Always called.")
}.catch { error in
  switch error {
    case SampleError.error:
      print("Some error")
    case SampleError.unexpected:
      print(error) // => Error Happened
    default:
      break
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment