Skip to content

Commit

Permalink
isPromise() thenable fix (#11048)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
cstringer and harsha509 committed Sep 29, 2022
1 parent e219c44 commit 316f973
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion javascript/node/selenium-webdriver/lib/util.js
Expand Up @@ -34,7 +34,16 @@ function isObject(value) {
* @return {boolean} Whether the value is a promise.
*/
function isPromise(value) {
return Object.prototype.toString.call(value) === '[object Promise]'
try {
// Use array notation so the Closure compiler does not obfuscate away our
// contract.
return (
(typeof value === 'object' || typeof value === 'function') &&
typeof value['then'] === 'function'
)
} catch (ex) {
return false
}
}

module.exports = {
Expand Down
5 changes: 5 additions & 0 deletions javascript/node/selenium-webdriver/test/lib/promise_test.js
Expand Up @@ -64,9 +64,14 @@ describe('promise', function () {
const x = new Promise(v, v)
const p = createRejectedPromise('reject')
const q = Promise.resolve('resolved')
const t = { then() {} }
const f = () => {}
f.then = () => {}
assert.equal(true, promise.isPromise(x))
assert.equal(true, promise.isPromise(p))
assert.equal(true, promise.isPromise(q))
assert.equal(true, promise.isPromise(t))
assert.equal(true, promise.isPromise(f))
assert.equal(false, promise.isPromise(0))
assert.equal(false, promise.isPromise(false))
assert.equal(false, promise.isPromise(true))
Expand Down

0 comments on commit 316f973

Please sign in to comment.