Closed
Description
Current implementaiton:
interface Promise<T> {
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>;
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): Promise<TResult>;
catch(onrejected?: (reason: any) => T | PromiseLike<T>): Promise<T>;
}
What I believe is a more correct implementation:
interface Promise<T> {
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>;
then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): Promise<TResult>;
catch<TResult>(onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Promise<TResult>;
catch<TResult>(onrejected?: (reason: any) => void): Promise<TResult>;
}
I believe the spec says that catch(x)
behaves the same as then(undefined, x)
, which implies that catch should be a generic method with a TResult
type.
If I am misunderstanding the spec, enlightenment would be appreciated.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
basarat commentedon Jul 13, 2015
catch<TResult>(onrejected?: (reason: any) => void): Promise<TResult>;
is wrong in that if you have acatch
and it does indeed end up getting called then ifonRejected
returnsvoid
the following promise chain will getPromise<void>
notPromise<TResult>
That said the promise definition is best effort and not designed to be 100% correct, but the change you are recommending is not one that should be done 🌹
basarat commentedon Jul 13, 2015
On second reading your suggestion isn't bad and maybe worth doing. I just wanted to point out that it is still best effort.
danquirk commentedon Jul 13, 2015
@rbuckton might have something to say here
MicahZoltu commentedon Jul 13, 2015
I'm actually not entirely sure what happens if you chain a promise to a catch that doesn't return. Does the original promise result get chained through? Does the chain get stopped prematurely?
Example:
I believe the answer to this question would shed some light on what the signature should look like.
basarat commentedon Jul 14, 2015
Here is a concrete example with comments to show what will happen:
Basically a true implementation starts to feel like JAVAs throws : http://stackoverflow.com/a/29287864/390330
RyanCavanaugh commentedon Jun 23, 2021
There doesn't seem to be much demand for this, and without sample code it's hard to evaluate what the deficit is.