diff --git a/javascript/node/selenium-webdriver/lib/promise.js b/javascript/node/selenium-webdriver/lib/promise.js index d59d9b7e783c8..fdd0f698335ff 100644 --- a/javascript/node/selenium-webdriver/lib/promise.js +++ b/javascript/node/selenium-webdriver/lib/promise.js @@ -1561,10 +1561,11 @@ function defer() { * @template T */ function fulfilled(opt_value) { - if (usePromiseManager()) { - return ManagedPromise.resolve(opt_value); + let ctor = usePromiseManager() ? ManagedPromise : NativePromise; + if (opt_value instanceof ctor) { + return /** @type {!Thenable} */(opt_value); } - return NativePromise.resolve(opt_value); + return ctor.resolve(opt_value); } @@ -2081,6 +2082,10 @@ function usePromiseManager() { /** + * Creates a new promise with the given `resolver` function. If the promise + * manager is currently enabled, the returned promise will be a + * {@linkplain ManagedPromise} instance. Otherwise, it will be a native promise. + * * @param {function( * function((T|IThenable|Thenable|null)=), * function(*=))} resolver @@ -3317,6 +3322,7 @@ module.exports = { consume: consume, controlFlow: controlFlow, createFlow: createFlow, + createPromise: createPromise, defer: defer, delayed: delayed, filter: filter, diff --git a/javascript/node/selenium-webdriver/test/lib/promise_test.js b/javascript/node/selenium-webdriver/test/lib/promise_test.js index 96d2ccc227468..8da1cd89e9fac 100644 --- a/javascript/node/selenium-webdriver/test/lib/promise_test.js +++ b/javascript/node/selenium-webdriver/test/lib/promise_test.js @@ -301,6 +301,25 @@ describe('promise', function() { }); promiseManagerSuite(() => { + describe('fulfilled', function() { + it('returns input value if it is already a valid promise', function() { + let p = promise.createPromise(function() {}); + let r = promise.fulfilled(p); + assert.strictEqual(p, r); + }); + + it('creates a new promise fulfilled with input', function() { + return promise.fulfilled(1234).then(v => assert.equal(1234, v)); + }); + + it('can convert thenables to valid promise', function() { + let thenable = {then: function(cb) {cb(1234)}}; + let p = promise.fulfilled(thenable); + assert.notStrictEqual(thenable, p); + return p.then(v => assert.equal(1234, v)); + }); + }); + describe('when', function() { it('ReturnsAResolvedPromiseIfGivenANonPromiseValue', function() { var ret = promise.when('abc');