File tree Expand file tree Collapse file tree
javascript/node/selenium-webdriver Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1561,10 +1561,11 @@ function defer() {
15611561 * @template T
15621562 */
15631563function fulfilled ( opt_value ) {
1564- if ( usePromiseManager ( ) ) {
1565- return ManagedPromise . resolve ( opt_value ) ;
1564+ let ctor = usePromiseManager ( ) ? ManagedPromise : NativePromise ;
1565+ if ( opt_value instanceof ctor ) {
1566+ return /** @type {!Thenable<T> } */ ( opt_value ) ;
15661567 }
1567- return NativePromise . resolve ( opt_value ) ;
1568+ return ctor . resolve ( opt_value ) ;
15681569}
15691570
15701571
@@ -2081,6 +2082,10 @@ function usePromiseManager() {
20812082
20822083
20832084/**
2085+ * Creates a new promise with the given `resolver` function. If the promise
2086+ * manager is currently enabled, the returned promise will be a
2087+ * {@linkplain ManagedPromise} instance. Otherwise, it will be a native promise.
2088+ *
20842089 * @param {function(
20852090 * function((T|IThenable<T>|Thenable|null)=),
20862091 * function(*=))} resolver
@@ -3317,6 +3322,7 @@ module.exports = {
33173322 consume : consume ,
33183323 controlFlow : controlFlow ,
33193324 createFlow : createFlow ,
3325+ createPromise : createPromise ,
33203326 defer : defer ,
33213327 delayed : delayed ,
33223328 filter : filter ,
Original file line number Diff line number Diff line change @@ -301,6 +301,25 @@ describe('promise', function() {
301301 } ) ;
302302
303303 promiseManagerSuite ( ( ) => {
304+ describe ( 'fulfilled' , function ( ) {
305+ it ( 'returns input value if it is already a valid promise' , function ( ) {
306+ let p = promise . createPromise ( function ( ) { } ) ;
307+ let r = promise . fulfilled ( p ) ;
308+ assert . strictEqual ( p , r ) ;
309+ } ) ;
310+
311+ it ( 'creates a new promise fulfilled with input' , function ( ) {
312+ return promise . fulfilled ( 1234 ) . then ( v => assert . equal ( 1234 , v ) ) ;
313+ } ) ;
314+
315+ it ( 'can convert thenables to valid promise' , function ( ) {
316+ let thenable = { then : function ( cb ) { cb ( 1234 ) } } ;
317+ let p = promise . fulfilled ( thenable ) ;
318+ assert . notStrictEqual ( thenable , p ) ;
319+ return p . then ( v => assert . equal ( 1234 , v ) ) ;
320+ } ) ;
321+ } ) ;
322+
304323 describe ( 'when' , function ( ) {
305324 it ( 'ReturnsAResolvedPromiseIfGivenANonPromiseValue' , function ( ) {
306325 var ret = promise . when ( 'abc' ) ;
You can’t perform that action at this time.
0 commit comments