From 157b8deda9afc4ca75b296608bc638b37f652172 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 20 May 2016 15:51:01 -0400 Subject: [PATCH] Allow processor passed to Zotero.HTTP.processDocuments to return a promise If a promise is returned, it's waited for before continuing with the next document. --- chrome/content/zotero/xpcom/http.js | 43 +++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/chrome/content/zotero/xpcom/http.js b/chrome/content/zotero/xpcom/http.js index 5a2ec0587..f8de939c1 100644 --- a/chrome/content/zotero/xpcom/http.js +++ b/chrome/content/zotero/xpcom/http.js @@ -790,7 +790,8 @@ Zotero.HTTP = new function() { * Load one or more documents in a hidden browser * * @param {String|String[]} urls URL(s) of documents to load - * @param {Function} processor Callback to be executed for each document loaded + * @param {Function} processor - Callback to be executed for each document loaded; if function returns + * a promise, it's waited for before continuing * @param {Function} done Callback to be executed after all documents have been loaded * @param {Function} exception Callback to be executed if an exception occurs * @param {Boolean} dontDelete Don't delete the hidden browser upon completion; calling function @@ -853,16 +854,40 @@ Zotero.HTTP = new function() { hiddenBrowser.removeEventListener("pageshow", onLoad, true); hiddenBrowser.zotero_loaded = true; + var maybePromise; + var error; try { - processor(doc); - } catch(e) { - if(exception) { - exception(e); - return; - } else { - throw(e); + maybePromise = processor(doc); + } + catch (e) { + error = e; + } + + // If processor returns a promise, wait for it + if (maybePromise.then) { + maybePromise.then(() => doLoad()) + .catch(e => { + if (exception) { + exception(e); + } + else { + throw e; + } + }); + return; + } + + try { + if (error) { + if (exception) { + exception(error); + } + else { + throw error; + } } - } finally { + } + finally { doLoad(); } };