Add functional shim for Zotero.Promise.method()

For translation.
This commit is contained in:
Abe Jellinek 2025-07-01 16:51:37 -04:00
parent da18a465c5
commit d035c94ae3

View file

@ -13,10 +13,29 @@ export class BluebirdShimPromise extends Promise {
}
static method(fn) {
// TEMP: "Implement" this with a stub until we update zotero/translate
// Don't bother with synchronous resolution - this is just enough to get the client to load
// ONLY exists for translator code that needs synchronous resolution.
// Please don't use this anywhere else. It's a bad idea!
return function (...args) {
return Promise.resolve(fn.apply(this, args));
try {
let returnValue = fn.apply(this, args);
let promise;
let isResolved = false;
if (returnValue && returnValue.then) {
promise = returnValue;
}
else {
promise = Promise.resolve(returnValue);
isResolved = true;
}
if (typeof promise.isResolved === 'undefined') {
promise.then(() => isResolved = true, () => {});
promise.isResolved = () => isResolved;
}
return promise;
}
catch (e) {
return Promise.reject(e);
}
};
}
}