Skip to content

Commit 316f973

Browse files
cstringerharsha509
andauthored
isPromise() thenable fix (#11048)
* Revert change to isPromise() (84dd610) * Add assertions to isPromise() test case for thenable object/function Co-authored-by: Sri Harsha <12621691+harsha509@users.noreply.github.com>
1 parent e219c44 commit 316f973

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

javascript/node/selenium-webdriver/lib/util.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ function isObject(value) {
3434
* @return {boolean} Whether the value is a promise.
3535
*/
3636
function isPromise(value) {
37-
return Object.prototype.toString.call(value) === '[object Promise]'
37+
try {
38+
// Use array notation so the Closure compiler does not obfuscate away our
39+
// contract.
40+
return (
41+
(typeof value === 'object' || typeof value === 'function') &&
42+
typeof value['then'] === 'function'
43+
)
44+
} catch (ex) {
45+
return false
46+
}
3847
}
3948

4049
module.exports = {

javascript/node/selenium-webdriver/test/lib/promise_test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ describe('promise', function () {
6464
const x = new Promise(v, v)
6565
const p = createRejectedPromise('reject')
6666
const q = Promise.resolve('resolved')
67+
const t = { then() {} }
68+
const f = () => {}
69+
f.then = () => {}
6770
assert.equal(true, promise.isPromise(x))
6871
assert.equal(true, promise.isPromise(p))
6972
assert.equal(true, promise.isPromise(q))
73+
assert.equal(true, promise.isPromise(t))
74+
assert.equal(true, promise.isPromise(f))
7075
assert.equal(false, promise.isPromise(0))
7176
assert.equal(false, promise.isPromise(false))
7277
assert.equal(false, promise.isPromise(true))

0 commit comments

Comments
 (0)