-
Notifications
You must be signed in to change notification settings - Fork 297
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
Ignore a certain Error and terminate Promise #201
Comments
I was about to say, use |
Breaking the chain like that seems to be against the goals of using promises and the code makes every attempt to not break the promise chain. Is this a common pattern you need? Why can't the catch block handle it, or some catch along the way can catch it and substitute a more appropriate error? |
A promise must always produce a value or an error. So Let say I store a Promise to trigger API request everytime users type to search box. So for every character, I should cancel the previous request. Because Promise does not offer a cancellation mechanism, so I reject the Promise with a func doSearch(query: String?) {
self.promise?.reject(PromiseCancelledError())
self.promise = doApiRequest(query: query)
} If I use this only in one place then sure I can catch I know that breaking the chain is not something |
Best way I can think of is an extension like this: extension Promise {
func catchIgnoreCancelled(_ handler: @escaping Catch) -> Promise<Value> {
self.catch({ error in
if error is PromiseCancelledError {
print("promise is cancelled")
} else {
handler(error)
}
})
}
} but every catches must be replace with this. |
Hmm, it seems that let promise: Promise<String> = Promise { fulfill, reject in
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
fulfill("done")
}
}
.then({ s in
print(s)
})
.catch({ error in
print(error)
})
promise.reject(PromiseCancelledError())
print("Promise:", promise.description) This code block still prints out Even this is not working: let promise = Promise<String>
.pending()
.then({ s in
print("=========================== then:", s)
})
.catch({ error in
print("=========================== catch:", error)
})
promise.reject(CustomError()) Nothing is printed out. When I debug into sourcecode, |
Is there any way to completely ignore an Error? For example, when I reject a Promise with something like
PromiseCancelledError
, I don't want the downstreamcatch
es to handle this error. I want the Promise to be terminated and produce no output at all. Bothcatch
andrecover
are not working for this case.The text was updated successfully, but these errors were encountered: