Skip to content

Commit

Permalink
Added reject polyfill and PromiseCapability
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodhkp committed Apr 29, 2014
1 parent 0d30bb0 commit 04b1eef
Showing 1 changed file with 48 additions and 6 deletions.
54 changes: 48 additions & 6 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,31 @@ var LegacyPromise = PDFJS.LegacyPromise = (function LegacyPromiseClosure() {
};
})();

/**
* Promise Capability object.
*
* @typedef {Object} PromiseCapability
* @property {Promise} promise - A promise object.
* @property {function} resolve - Fullfills the promise.
* @property {function} reject - Rejects the promise.
*/

/**
* Creates a promise capability object.
* @return {PromiseCapability} A capability object contains:
* - a Promise, resolve and reject methods.
*/
function createPromiseCapability() {
var capability = {};
capability.promise = new Promise(function (resolve, reject) {
capability.resolve = resolve;
capability.reject = reject;
});
return capability;
}

PDFJS.createPromiseCapability = createPromiseCapability;

/**
* Polyfill for Promises:
* The following promise implementation tries to generally implment the
Expand Down Expand Up @@ -988,8 +1013,15 @@ var LegacyPromise = PDFJS.LegacyPromise = (function LegacyPromiseClosure() {
};
}
if (typeof globalScope.Promise.resolve !== 'function') {
globalScope.Promise.resolve = function (x) {
return new globalScope.Promise(function (resolve) { resolve(x); });
globalScope.Promise.resolve = function (value) {
return new globalScope.Promise(function (resolve) { resolve(value); });
};
}
if (typeof globalScope.Promise.reject !== 'function') {
globalScope.Promise.reject = function (reason) {
return new globalScope.Promise(function (resolve, reject) {
reject(reason);
});
};
}
return;
Expand Down Expand Up @@ -1167,18 +1199,28 @@ var LegacyPromise = PDFJS.LegacyPromise = (function LegacyPromiseClosure() {

/**
* Checks if the value is likely a promise (has a 'then' function).
* @return {boolean} true if x is thenable
* @return {boolean} true if value is thenable
*/
Promise.isPromise = function Promise_isPromise(value) {
return value && typeof value.then === 'function';
};

/**
* Creates resolved promise
* @param x resolve value
* @param value resolve value
* @returns {Promise}
*/
Promise.resolve = function Promise_resolve(value) {
return new Promise(function (resolve) { resolve(value); });
};

/**
* Creates rejected promise
* @param reason rejection value
* @returns {Promise}
*/
Promise.resolve = function Promise_resolve(x) {
return new Promise(function (resolve) { resolve(x); });
Promise.reject = function Promise_reject(reason) {
return new Promise(function (resolve, reject) { reject(reason); });
};

Promise.prototype = {
Expand Down

0 comments on commit 04b1eef

Please sign in to comment.