Skip to content

Commit b8a8732

Browse files
committed
[js] Add promise.Promise#catch() for API compat with native promises
1 parent 403527c commit b8a8732

File tree

5 files changed

+52
-15
lines changed

5 files changed

+52
-15
lines changed

javascript/node/selenium-webdriver/CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v2.49.0-dev
2+
3+
* Added `promise.Promise#catch()` for API compatibility with native Promises.
4+
`promise.Promise#thenCatch()` is not yet deprecated, but it simply
5+
delegates to `catch`.
6+
17
## v2.48.2
28

39
* Added `WebElement#takeScreenshot()`.

javascript/node/selenium-webdriver/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "selenium-webdriver",
3-
"version": "2.48.2",
3+
"version": "2.49.0-dev",
44
"description": "The official WebDriver JavaScript bindings from the Selenium project",
55
"license": "Apache-2.0",
66
"keywords": [

javascript/webdriver/promise.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@
485485
* // No callbacks registered on promise -> unhandled rejection
486486
* promise.rejected(Error('boom'));
487487
* flow.execute(function() { console.log('this will never run'); });
488-
* }).thenCatch(function(e) {
488+
* }).catch(function(e) {
489489
* console.log(e.message);
490490
* });
491491
* // boom
@@ -497,7 +497,7 @@
497497
* promise.rejected(Error('boom'));
498498
* flow.execute(function() { console.log('a'); }).
499499
* then(function() { console.log('b'); });
500-
* }).thenCatch(function(e) {
500+
* }).catch(function(e) {
501501
* console.log(e.message);
502502
* });
503503
* // boom
@@ -508,7 +508,7 @@
508508
* flow.execute(function() {
509509
* promise.rejected(Error('boom'));
510510
* return flow.execute(someOtherTask);
511-
* }).thenCatch(function(e) {
511+
* }).catch(function(e) {
512512
* console.log(e.message);
513513
* });
514514
* // boom
@@ -521,7 +521,7 @@
521521
* flow.execute(function() {
522522
* promise.rejected(Error('boom1'));
523523
* promise.rejected(Error('boom2'));
524-
* }).thenCatch(function(ex) {
524+
* }).catch(function(ex) {
525525
* console.log(ex instanceof promise.MultipleUnhandledRejectionError);
526526
* for (var e of ex.errors) {
527527
* console.log(e.message);
@@ -542,7 +542,7 @@
542542
* flow.execute(function() {
543543
* promise.rejected(Error('boom'));
544544
* subTask = flow.execute(function() {});
545-
* }).thenCatch(function(e) {
545+
* }).catch(function(e) {
546546
* console.log(e.message);
547547
* }).then(function() {
548548
* return subTask.then(
@@ -564,7 +564,7 @@
564564
* throw Error('fail!');
565565
* });
566566
* });
567-
* }).thenCatch(function(e) {
567+
* }).catch(function(e) {
568568
* console.log(e.message);
569569
* });
570570
* // fail!
@@ -920,7 +920,7 @@ promise.Thenable = goog.defineClass(null, {
920920
* }
921921
*
922922
* // Asynchronous promise API:
923-
* doAsynchronousWork().thenCatch(function(ex) {
923+
* doAsynchronousWork().catch(function(ex) {
924924
* console.error(ex);
925925
* });
926926
*
@@ -931,6 +931,18 @@ promise.Thenable = goog.defineClass(null, {
931931
* resolved with the result of the invoked callback.
932932
* @template R
933933
*/
934+
catch: function(errback) {},
935+
936+
/**
937+
* An alias for {@link #catch()}
938+
*
939+
* @param {function(*): (R|IThenable<R>)} errback The
940+
* function to call if this promise is rejected. The function should
941+
* expect a single argument: the rejection reason.
942+
* @return {!promise.Promise<R>} A new promise which will be
943+
* resolved with the result of the invoked callback.
944+
* @template R
945+
*/
934946
thenCatch: function(errback) {},
935947

936948
/**
@@ -1247,9 +1259,14 @@ promise.Promise = goog.defineClass(null, {
12471259
},
12481260

12491261
/** @override */
1250-
thenCatch: function(errback) {
1262+
catch: function(errback) {
12511263
return this.addCallback_(
1252-
null, errback, 'thenCatch', promise.Promise.prototype.thenCatch);
1264+
null, errback, 'catch', promise.Promise.prototype.catch);
1265+
},
1266+
1267+
/** @override */
1268+
thenCatch: function(errback) {
1269+
return this.catch(errback);
12531270
},
12541271

12551272
/** @override */
@@ -1419,6 +1436,14 @@ promise.Deferred = goog.defineClass(null, {
14191436
return this.promise.then(opt_cb, opt_eb);
14201437
},
14211438

1439+
/**
1440+
* @override
1441+
* @deprecated Use {@lcode catch} from the promise property directly.
1442+
*/
1443+
catch: function(opt_eb) {
1444+
return this.promise.catch(opt_eb);
1445+
},
1446+
14221447
/**
14231448
* @override
14241449
* @deprecated Use {@code thenCatch} from the promise property directly.
@@ -1484,7 +1509,7 @@ promise.delayed = function(ms) {
14841509
key = null;
14851510
fulfill();
14861511
}, ms);
1487-
}).thenCatch(function(e) {
1512+
}).catch(function(e) {
14881513
clearTimeout(key);
14891514
key = null;
14901515
throw e;
@@ -2992,7 +3017,7 @@ promise.isGenerator = function(fn) {
29923017
* yield promise.delayed(250).then(function() {
29933018
* throw Error('boom');
29943019
* });
2995-
* }).thenCatch(function(e) {
3020+
* }).catch(function(e) {
29963021
* console.log(e.toString()); // Error: boom
29973022
* });
29983023
*

javascript/webdriver/test/promise_test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,9 +1948,9 @@ function testLongStackTraces_appendsEachStepToRejectionError() {
19481948
assertArrayEquals([
19491949
'From: Promise: new',
19501950
'From: Promise: then',
1951-
'From: Promise: thenCatch',
1951+
'From: Promise: catch',
19521952
'From: Promise: then',
1953-
'From: Promise: thenCatch',
1953+
'From: Promise: catch',
19541954
], getStackMessages(e));
19551955
});
19561956

@@ -1989,7 +1989,7 @@ function testLongStackTraces_errorOccursInCallbackChain() {
19891989
goog.string.startsWith(e.stack, originalStack));
19901990
assertArrayEquals([
19911991
'From: Promise: then',
1992-
'From: Promise: thenCatch',
1992+
'From: Promise: catch',
19931993
], getStackMessages(e));
19941994
});
19951995

javascript/webdriver/webdriver.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,6 +2269,9 @@ webdriver.WebElementPromise = function(driver, el) {
22692269
/** @override */
22702270
this.then = goog.bind(el.then, el);
22712271

2272+
/** @override */
2273+
this.catch = goog.bind(el.catch, el);
2274+
22722275
/** @override */
22732276
this.thenCatch = goog.bind(el.thenCatch, el);
22742277

@@ -2392,6 +2395,9 @@ webdriver.AlertPromise = function(driver, alert) {
23922395
/** @override */
23932396
this.then = goog.bind(alert.then, alert);
23942397

2398+
/** @override */
2399+
this.catch = goog.bind(alert.catch, alert);
2400+
23952401
/** @override */
23962402
this.thenCatch = goog.bind(alert.thenCatch, alert);
23972403

0 commit comments

Comments
 (0)