mirror of
https://github.com/zotero/zotero.git
synced 2026-09-30 01:51:36 +00:00
Update ConcurrentCaller to use native promises
This commit is contained in:
parent
0d45a8b4db
commit
ba5007c4e7
2 changed files with 12 additions and 10 deletions
|
|
@ -227,11 +227,11 @@ Zotero.Sync.Storage.Engine.prototype.start = async function () {
|
|||
let failed = 0;
|
||||
|
||||
for (let p of results) {
|
||||
if (p.isFulfilled()) {
|
||||
try {
|
||||
await p;
|
||||
succeeded++;
|
||||
}
|
||||
else if (!p.isPending()) {
|
||||
let e = p.reason();
|
||||
catch (e) {
|
||||
if (e instanceof Zotero.HTTP.CancelledException) {
|
||||
Zotero.debug(`File ${type} sync cancelled for ${this.library.name} `
|
||||
+ `(${succeeded} succeeded, ${failed} failed)`);
|
||||
|
|
|
|||
|
|
@ -115,19 +115,21 @@ ConcurrentCaller.prototype.add = function (func) {
|
|||
if (Array.isArray(func)) {
|
||||
let promises = [];
|
||||
for (let i = 0; i < func.length; i++) {
|
||||
promises.push(this.start(func[i]).reflect());
|
||||
promises.push(this.start(func[i]));
|
||||
}
|
||||
return Promise.all(promises);
|
||||
return Promise.allSettled(promises).then(() => promises);
|
||||
}
|
||||
|
||||
if (!this._deferred || !this._deferred.promise.isPending()) {
|
||||
if (!this._deferred) {
|
||||
this._deferred = Promise.defer();
|
||||
let onResolve = () => this._deferred = null;
|
||||
this._deferred.promise.then(onResolve, onResolve);
|
||||
}
|
||||
|
||||
var deferred = Promise.defer();
|
||||
this._queue.push({
|
||||
func: Promise.method(func),
|
||||
deferred: deferred
|
||||
func,
|
||||
deferred
|
||||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
|
|
@ -151,7 +153,7 @@ ConcurrentCaller.prototype.start = function (func) {
|
|||
/**
|
||||
* Start processing if not already running and wait for all tasks to complete
|
||||
*
|
||||
* @return {Promise[]} - An array of promises for all currently queued tasks
|
||||
* @return {Promise<Promise[]>} - An array of promises for all currently queued tasks
|
||||
*/
|
||||
ConcurrentCaller.prototype.runAll = function () {
|
||||
// If nothing queued, return immediately
|
||||
|
|
@ -163,7 +165,7 @@ ConcurrentCaller.prototype.runAll = function () {
|
|||
var run = this._processNext();
|
||||
}
|
||||
while (run);
|
||||
return this._deferred.promise.return(promises);
|
||||
return this._deferred.promise.then(() => promises);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue