PromiseUtils.jsm

This article needs a technical review. How you can help.

The PromiseUtils.jsm JavaScript code module offers some useful functions related to DOM Promise. To use it, you first need to import the code module into your JavaScript scope:

Components.utils.import("resource://gre/modules/PromiseUtils.jsm");

Method overview

Promise resolveOrTimeout(promise, delay, dejection);Obsolete since Gecko 38.0
Deferred defer();

Methods

resolveOrTimeout()

PromiseUtils.resolveOrTimeout offers a simple timeout mechanism for Promise.

Promise resolveOrTimeout(
  promise,
  delay,
  rejection
);
Parameters
promise
(Promise) The Promise that should resolve/reject quickly.
delay
(number) A delay after which to stop waiting for promise, in milliseconds.
rejection
(function) If promise hasn't resolved/rejected after delay, a value used to construct the rejection.
Return value

A promise that behaves as promise, if promise is resolved/rejected within delay ms, or rejects with rejection() otherwise.

Examples

Following code assigns a promise which behaves as myModule.shutdown(), but timed out after 1000 ms.

let p = PromiseUtils.resolveOrTimeout(
  myModule.shutdown(),
  1000,
  () => new Error("The module took too long to shutdown")
);

defer()

Creates a new pending Promise and provides methods to resolve or reject this Promise.

This method was previously implemented as Promise.defer(), which is obsolete since Gecko 30. And PromiseUtils.defer uses DOM Promise instead of Promise.jsm's Promise.

Deferred defer();
Parameters

None.

Return value

A new object, containing the new promise in the promise property, and the methods to change its state in the resolve and reject properties. See the Deferred documentation for details.

See also

Document Tags and Contributors

 Contributors to this page: kitcambridge, kscarfone, arai
 Last updated by: kitcambridge,