mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-08-20 12:00:53 +08:00
Added better catch definitions that pass through the original promise type or type union of promise|rejection type
This commit is contained in:
+71
-10
@@ -80,6 +80,7 @@ var voidProm: Promise<void>;
|
||||
|
||||
var fooProm: Promise<Foo>;
|
||||
var barProm: Promise<Bar>;
|
||||
var fooOrBarProm: Promise<Foo|Bar>;
|
||||
var bazProm: Promise<Baz>;
|
||||
|
||||
// - - - - - - - - - - - - - - - - -
|
||||
@@ -237,36 +238,96 @@ barProm = barProm.then((value: Bar) => {
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
barProm = fooProm.catch((reason: any) => {
|
||||
fooProm = fooProm.catch((reason: any) => {
|
||||
return;
|
||||
});
|
||||
|
||||
fooProm = fooProm.caught((reason: any) => {
|
||||
return;
|
||||
});
|
||||
fooProm = fooProm.catch((error: any) => {
|
||||
return true;
|
||||
}, (reason: any) => {
|
||||
return;
|
||||
});
|
||||
fooProm = fooProm.caught((error: any) => {
|
||||
return true;
|
||||
}, (reason: any) => {
|
||||
return;
|
||||
});
|
||||
|
||||
fooProm = fooProm.catch((reason: any) => {
|
||||
return voidProm;
|
||||
});
|
||||
|
||||
fooProm = fooProm.caught((reason: any) => {
|
||||
return voidProm;
|
||||
});
|
||||
fooProm = fooProm.catch((error: any) => {
|
||||
return true;
|
||||
}, (reason: any) => {
|
||||
return voidProm;
|
||||
});
|
||||
fooProm = fooProm.caught((error: any) => {
|
||||
return true;
|
||||
}, (reason: any) => {
|
||||
return voidProm;
|
||||
});
|
||||
|
||||
fooProm = fooProm.catch((reason: any) => {
|
||||
//handle multiple valid return types simultaneously
|
||||
if (true) {
|
||||
return;
|
||||
} else if (false) {
|
||||
return voidProm;
|
||||
} else if (foo) {
|
||||
return foo;
|
||||
}
|
||||
});
|
||||
|
||||
fooOrBarProm = fooProm.catch((reason: any) => {
|
||||
return bar;
|
||||
});
|
||||
barProm = fooProm.caught((reason: any) => {
|
||||
fooOrBarProm = fooProm.caught((reason: any) => {
|
||||
return bar;
|
||||
});
|
||||
|
||||
barProm = fooProm.catch((reason: any) => {
|
||||
return bar;
|
||||
fooOrBarProm = fooProm.catch((error: any) => {
|
||||
return true;
|
||||
}, (reason: any) => {
|
||||
return bar;
|
||||
});
|
||||
barProm = fooProm.caught((reason: any) => {
|
||||
return bar;
|
||||
fooOrBarProm = fooProm.caught((error: any) => {
|
||||
return true;
|
||||
}, (reason: any) => {
|
||||
return bar;
|
||||
});
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
barProm = fooProm.catch(Error, (reason: any) => {
|
||||
fooProm = fooProm.catch(Error, (reason: any) => {
|
||||
return;
|
||||
});
|
||||
fooProm = fooProm.catch(Promise.CancellationError, (reason: any) => {
|
||||
return;
|
||||
});
|
||||
fooProm = fooProm.caught(Error, (reason: any) => {
|
||||
return;
|
||||
});
|
||||
fooProm = fooProm.caught(Promise.CancellationError, (reason: any) => {
|
||||
return;
|
||||
});
|
||||
|
||||
fooOrBarProm = fooProm.catch(Error, (reason: any) => {
|
||||
return bar;
|
||||
});
|
||||
barProm = fooProm.catch(Promise.CancellationError, (reason: any) => {
|
||||
fooOrBarProm = fooProm.catch(Promise.CancellationError, (reason: any) => {
|
||||
return bar;
|
||||
});
|
||||
barProm = fooProm.caught(Error, (reason: any) => {
|
||||
fooOrBarProm = fooProm.caught(Error, (reason: any) => {
|
||||
return bar;
|
||||
});
|
||||
barProm = fooProm.caught(Promise.CancellationError, (reason: any) => {
|
||||
fooOrBarProm = fooProm.caught(Promise.CancellationError, (reason: any) => {
|
||||
return bar;
|
||||
});
|
||||
|
||||
|
||||
Vendored
+13
-12
@@ -33,11 +33,11 @@ declare class Promise<R> implements Promise.Thenable<R>, Promise.Inspection<R> {
|
||||
*
|
||||
* Alias `.caught();` for compatibility with earlier ECMAScript version.
|
||||
*/
|
||||
catch<U>(onReject?: (error: any) => Promise.Thenable<U>): Promise<U>;
|
||||
caught<U>(onReject?: (error: any) => Promise.Thenable<U>): Promise<U>;
|
||||
catch(onReject?: (error: any) => R|Promise.Thenable<R>|void|Promise.Thenable<void>): Promise<R>;
|
||||
caught(onReject?: (error: any) => R|Promise.Thenable<R>|void|Promise.Thenable<void>): Promise<R>;
|
||||
|
||||
catch<U>(onReject?: (error: any) => U): Promise<U>;
|
||||
caught<U>(onReject?: (error: any) => U): Promise<U>;
|
||||
catch<U>(onReject?: (error: any) => U|Promise.Thenable<U>): Promise<U|R>;
|
||||
caught<U>(onReject?: (error: any) => U|Promise.Thenable<U>): Promise<U|R>;
|
||||
|
||||
/**
|
||||
* This extends `.catch` to work more like catch-clauses in languages like Java or C#. Instead of manually checking `instanceof` or `.name === "SomeError"`, you may specify a number of error constructors which are eligible for this catch handler. The catch handler that is first met that has eligible constructors specified, is the one that will be called.
|
||||
@@ -46,17 +46,18 @@ declare class Promise<R> implements Promise.Thenable<R>, Promise.Inspection<R> {
|
||||
*
|
||||
* Alias `.caught();` for compatibility with earlier ECMAScript version.
|
||||
*/
|
||||
catch<U>(predicate: (error: any) => boolean, onReject: (error: any) => Promise.Thenable<U>): Promise<U>;
|
||||
caught<U>(predicate: (error: any) => boolean, onReject: (error: any) => Promise.Thenable<U>): Promise<U>;
|
||||
catch(predicate: (error: any) => boolean, onReject: (error: any) => R|Promise.Thenable<R>|void|Promise.Thenable<void>): Promise<R>;
|
||||
caught(predicate: (error: any) => boolean, onReject: (error: any) => R|Promise.Thenable<R>|void|Promise.Thenable<void>): Promise<R>;
|
||||
|
||||
catch<U>(predicate: (error: any) => boolean, onReject: (error: any) => U): Promise<U>;
|
||||
caught<U>(predicate: (error: any) => boolean, onReject: (error: any) => U): Promise<U>;
|
||||
catch<U>(predicate: (error: any) => boolean, onReject: (error: any) => U|Promise.Thenable<U>): Promise<U|R>;
|
||||
caught<U>(predicate: (error: any) => boolean, onReject: (error: any) => U|Promise.Thenable<U>): Promise<U|R>;
|
||||
|
||||
catch<U>(ErrorClass: Function, onReject: (error: any) => Promise.Thenable<U>): Promise<U>;
|
||||
caught<U>(ErrorClass: Function, onReject: (error: any) => Promise.Thenable<U>): Promise<U>;
|
||||
catch(ErrorClass: Function, onReject: (error: any) => R|Promise.Thenable<R>|void|Promise.Thenable<void>): Promise<R>;
|
||||
caught(ErrorClass: Function, onReject: (error: any) => R|Promise.Thenable<R>|void|Promise.Thenable<void>): Promise<R>;
|
||||
|
||||
catch<U>(ErrorClass: Function, onReject: (error: any) => U|Promise.Thenable<U>): Promise<U|R>;
|
||||
caught<U>(ErrorClass: Function, onReject: (error: any) => U|Promise.Thenable<U>): Promise<U|R>;
|
||||
|
||||
catch<U>(ErrorClass: Function, onReject: (error: any) => U): Promise<U>;
|
||||
caught<U>(ErrorClass: Function, onReject: (error: any) => U): Promise<U>;
|
||||
|
||||
/**
|
||||
* Like `.catch` but instead of catching all types of exceptions, it only catches those that don't originate from thrown errors but rather from explicit rejections.
|
||||
|
||||
Reference in New Issue
Block a user