1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 var EXPORTED_SYMBOLS = ["PromiseUtils"];
9 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
13 * Creates a new pending Promise and provide methods to resolve and reject this Promise.
15 * @return {Deferred} an object consisting of a pending Promise "promise"
16 * and methods "resolve" and "reject" to change its state.
19 return new Deferred();
23 * Requests idle dispatch to the main thread for the given callback,
24 * and returns a promise which resolves to the callback's return value
25 * when it has been executed.
27 * @param {function} callback
28 * @param {integer} [timeout]
29 * An optional timeout, after which the callback will be
30 * executed immediately if idle dispatch has not yet occurred.
34 idleDispatch(callback, timeout = 0) {
35 return new Promise((resolve, reject) => {
36 Services.tm.idleDispatchToMainThread(() => {
48 * The definition of Deferred object which is returned by PromiseUtils.defer(),
49 * It contains a Promise and methods to resolve/reject it.
52 /* A method to resolve the associated Promise with the value passed.
53 * If the promise is already settled it does nothing.
55 * @param {anything} value : This value is used to resolve the promise
56 * If the value is a Promise then the associated promise assumes the state
57 * of Promise passed as value.
61 /* A method to reject the assocaited Promise with the value passed.
62 * If the promise is already settled it does nothing.
64 * @param {anything} reason: The reason for the rejection of the Promise.
65 * Generally its an Error object. If however a Promise is passed, then the Promise
66 * itself will be the reason for rejection no matter the state of the Promise.
70 /* A newly created Pomise object.
71 * Initially in pending state.
73 this.promise = new Promise((resolve, reject) => {
74 this.resolve = resolve;