mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-23 08:38:06 +02:00
Add a settled
property, tracking the fulfilled/rejected stated of the Promise, to createPromiseCapability
This allows cleaning-up code which is currently manually tracking the state of the Promise of a `createPromiseCapability` instance.
This commit is contained in:
parent
291e62b41e
commit
22468817e1
4 changed files with 62 additions and 27 deletions
|
@ -972,23 +972,36 @@ function isSpace(ch) {
|
|||
* Promise Capability object.
|
||||
*
|
||||
* @typedef {Object} PromiseCapability
|
||||
* @property {Promise} promise - A promise object.
|
||||
* @property {function} resolve - Fulfills the promise.
|
||||
* @property {function} reject - Rejects the promise.
|
||||
* @property {Promise} promise - A Promise object.
|
||||
* @property {boolean} settled - If the Promise has been fulfilled/rejected.
|
||||
* @property {function} resolve - Fulfills the Promise.
|
||||
* @property {function} reject - Rejects the Promise.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a promise capability object.
|
||||
* @alias createPromiseCapability
|
||||
*
|
||||
* @return {PromiseCapability} A capability object contains:
|
||||
* - a Promise, resolve and reject methods.
|
||||
* @return {PromiseCapability}
|
||||
*/
|
||||
function createPromiseCapability() {
|
||||
var capability = {};
|
||||
capability.promise = new Promise(function (resolve, reject) {
|
||||
capability.resolve = resolve;
|
||||
capability.reject = reject;
|
||||
const capability = Object.create(null);
|
||||
let isSettled = false;
|
||||
|
||||
Object.defineProperty(capability, 'settled', {
|
||||
get() {
|
||||
return isSettled;
|
||||
},
|
||||
});
|
||||
capability.promise = new Promise(function(resolve, reject) {
|
||||
capability.resolve = function(data) {
|
||||
isSettled = true;
|
||||
resolve(data);
|
||||
};
|
||||
capability.reject = function(reason) {
|
||||
isSettled = true;
|
||||
reject(reason);
|
||||
};
|
||||
});
|
||||
return capability;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue