Namespace: webdriver.promise
View Source

# class Deferred<T>

All implemented interfaces
  `IThenable<T>`
  `webdriver.promise.Thenable<T>`

Represents a value that will be resolved at some point in the future. This class represents the protected "producer" half of a Promise - each Deferred has a `promise` property that may be returned to consumers for registering callbacks, reserving the ability to resolve the deferred to the producer.

If this Deferred is rejected and there are no listeners registered before the next turn of the event loop, the rejection will be passed to the `webdriver.promise.ControlFlow` as an unhandled failure.

### new Deferred(opt_flow)

Parameters

opt_flow`?webdriver.promise.ControlFlow=`

  The control flow this instance was created under. This should only be provided during unit tests.

## Instance Methods

### cancel(opt_reason)code »

Cancels the computation of this promise's value, rejecting the promise in the process. This method is a no-op if the promise has already been resolved.

Specified by: webdriver.promise.Thenable

Parameters

opt_reason`?(string|webdriver.promise.CancellationError)=`

  The reason this promise is being cancelled.

### fulfill(opt_value)code »

Resolves this deferred with the given value. It is safe to call this as a normal function (with no bound "this").

Parameters

opt_value`?(T|{then: ?})=`

  The fulfilled value.

### isPending()code »

Specified by: webdriver.promise.Thenable

Returns

`boolean`

  Whether this promise's value is still being computed.

### reject(opt_reason)code »

Rejects this promise with the given reason. It is safe to call this as a normal function (with no bound "this").

Parameters

opt_reason`*=`

  The rejection reason.

### then(opt_callback, opt_errback)code »

deprecated

Registers listeners for when this instance is resolved.

Specified by: webdriver.promise.Thenable, IThenable

Deprecated

  Use `then` from the promise property directly.

Parameters

opt_callback`?function(T): (R|IThenable<R>)=`

  The function to call if this promise is successfully resolved. The function should expect a single argument: the promise's resolved value.

opt_errback`?function(*): (R|IThenable<R>)=`

  The function to call if this promise is rejected. The function should expect a single argument: the rejection reason.

Returns

`webdriver.promise.Promise`

  A new promise which will be resolved with the result of the invoked callback.

### thenCatch(errback)code »

deprecated

Registers a listener for when this promise is rejected. This is synonymous with the `catch` clause in a synchronous API:

    // Synchronous API:
    try {
      doSynchronousWork();
    } catch (ex) {
      console.error(ex);
    }

    // Asynchronous promise API:
    doAsynchronousWork().thenCatch(function(ex) {
      console.error(ex);
    });

Specified by: webdriver.promise.Thenable

Deprecated

  Use `thenCatch` from the promise property directly.

Parameters

errback`function(*): (R|IThenable<R>)`

  The function to call if this promise is rejected. The function should expect a single argument: the rejection reason.

Returns

`webdriver.promise.Promise`

  A new promise which will be resolved with the result of the invoked callback.

### thenFinally(callback)code »

deprecated

Registers a listener to invoke when this promise is resolved, regardless of whether the promise's value was successfully computed. This function is synonymous with the `finally` clause in a synchronous API:

    // Synchronous API:
    try {
      doSynchronousWork();
    } finally {
      cleanUp();
    }

    // Asynchronous promise API:
    doAsynchronousWork().thenFinally(cleanUp);

Note: similar to the `finally` clause, if the registered callback returns a rejected promise or throws an error, it will silently replace the rejection error (if any) from this promise:

    try {
      throw Error('one');
    } finally {
      throw Error('two');  // Hides Error: one
    }

    promise.rejected(Error('one'))
        .thenFinally(function() {
          throw Error('two');  // Hides Error: one
        });

Specified by: webdriver.promise.Thenable

Deprecated

  Use `thenFinally` from the promise property directly.

Parameters

callback`function(): (R|IThenable<R>)`

  The function to call when this promise is resolved.

Returns

`webdriver.promise.Promise`

  A promise that will be fulfilled with the callback result.

## Instance Properties

promise `webdriver.promise.Promise<T>`
  No description.