0

How I can make Optional promises chaining? e.g., I have two promises and I have to check second only by some conditions from first promise results.

promise1(). then { result -> Promise? in
if result.success {
return promise2()
}
return nil
}
.then { secondResult -> Void in

}
.always {...}
.catch ...

But when I write something like this compiler thinks that type of is Promise?, not Any (bit "Any" I mean any other class/structure)

So, how I can make some kind of chaining? And there should be one always and one catch handlers as usual.

Roman Volkov
  • 245
  • 1
  • 4
  • 12

1 Answers1

0

I think you want something like this:

let p = Promise<Int>() { fulffill, reject in
  fulffill(0)
}.then { result -> Promise<Any>? in
  if result == 0 {
    return Promise(value: 5)
  } else if result == 1 {
    return Promise(value: "test")
  } else {
    return nil
  }
}.then { secondResult -> Void in
  print("\(secondResult?.value)")
}.always {
  print("Final!")
}

If result is 0 the secondResult.value will be Int with value of 5, if result is 1 the secondResult.value will be "test". Then you can use the result in the second Promise to make the decision.

vbgd
  • 1,937
  • 1
  • 13
  • 18