Skip to content

Commit

Permalink
[js] Export promise.createPromise(), which will return a managed prom…
Browse files Browse the repository at this point in the history
…ise if

the promise manager is enabled, otherwise it will return a native promise.

For #2969
  • Loading branch information
jleyba committed Jan 30, 2017
1 parent 7427778 commit 3d84143
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
12 changes: 9 additions & 3 deletions javascript/node/selenium-webdriver/lib/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>} */(opt_value);
}
return NativePromise.resolve(opt_value);
return ctor.resolve(opt_value);
}


Expand Down Expand Up @@ -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<T>|Thenable|null)=),
* function(*=))} resolver
Expand Down Expand Up @@ -3317,6 +3322,7 @@ module.exports = {
consume: consume,
controlFlow: controlFlow,
createFlow: createFlow,
createPromise: createPromise,
defer: defer,
delayed: delayed,
filter: filter,
Expand Down
19 changes: 19 additions & 0 deletions javascript/node/selenium-webdriver/test/lib/promise_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 3d84143

Please sign in to comment.