Shopify / promise-swift

Lightweight Promise implementation in swift.
MIT License
17 stars 9 forks source link

Question: how `cancel`works for chain of Promises? #3

Open gavrix opened 7 years ago

gavrix commented 7 years ago

Question from @psycotica0-shopify:

When chaining multiple promises together, how should the cancel be expected to behave? Will it call the cancel function of all items, or only the step it's on, or the step it's on an all that follow (are still pending), etc?

gavrix commented 7 years ago

When chaining multiple promises together, each step only triggers execution when previous step is complete. That means, for following promises cancellation token hasn't even been created yet until current finishes. Calling cancel will call cancellation action on currently executing Promise and will prevent following promises to be started.

Although not part of this question, it's worth noting that following Promises in the chain don't exist until current step completes. E.g.:

let chain = makeAsyncPromise()
  .then { step1_result in
    return makeAsyncPromise2(step1_result) // #1
  }
  .then { step2_result in
    return makeAsyncPromise3(step2_result) //#2
  }
  .whenComplete { step3_result in
    print("Final value from step3 is \(step3_result)")
  }

line #1 will not be called (and Promise instance won't be created) until step1 resolves. Similarly, line #2 will not be called (and Promise instance won't be created) until step2 resolves.

psycotica0-shopify commented 7 years ago

Oh, right! That's a good point I should have caught.

Now I'm not sure if it's worth putting that in the doc, because other people might wonder, or not, because it's obvious now that my head is screwed on straight.