Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I cancel a promise? #52

Closed
dinghaoz opened this issue Jun 17, 2018 · 4 comments
Closed

How do I cancel a promise? #52

dinghaoz opened this issue Jun 17, 2018 · 4 comments

Comments

@dinghaoz
Copy link

dinghaoz commented Jun 17, 2018

A use case is autocomplete. When user types, I send out a request to query the completion info but if the user types fast enough, I want to cancel previous sent request before sending the new one.

Thanks

@shoumikhin
Copy link
Contributor

Hi @dinghaoz, could you by chance throttle requests on the UI level?
Perhaps something like the following may work?

func textViewDidChange(_ textView: UITextView) {
  NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(autocomplete(_:)), object: textView)
  perform(#selector(autocomplete(_:)), with: textView, afterDelay: 0.5)
}

@objc
func autocomplete(_ textView: UITextView) {
  // ...
}

@dinghaoz
Copy link
Author

Thanks for the quick reply @shoumikhin, and sorry for the confusion.
Your solution solved the problem of sending redundant requests, however for the requests that have already been sent, they are not canceled (i.e. the completion callback will still be called). This might be ok when the autocompletion only involves one request. But if the autocompletion (for example), requires several requests to get the data, which are chained together with the promise pattern, how do I cancel all of them, including those haven't been sent out yet?

Think about another case where there are many depending network requests chained together with the promises pattern inside a UIViewController, and then when the user tap the back button of the UIViewController, I would like to cancel all of them, including those haven't been sent out yet, with one cancellation method on some object.

Does promises supports that? or do I use it in a wrong way?

@shoumikhin
Copy link
Contributor

There's some ongoing work to add convenient cancellation to Promises.
But for now, you'd need to use some kind of an outer cancellation token to set when the next request is issued to cancel the current one and probably use such token inside a promise work block to check if any work still needs to be done and stay notified when to bail out. As an option, you may also store a promise somewhere and directly reject it to skip any chained then blocks.

@dinghaoz
Copy link
Author

Thanks @shoumikhin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants