Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isPromise() thenable fix #11048

Merged
merged 6 commits into from Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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