diff --git a/chrome/content/zotero/xpcom/db.js b/chrome/content/zotero/xpcom/db.js index 707c38bd09..4fe3d7bfdb 100644 --- a/chrome/content/zotero/xpcom/db.js +++ b/chrome/content/zotero/xpcom/db.js @@ -586,22 +586,14 @@ Zotero.DBConnection.prototype.queryAsync = Zotero.Promise.coroutine(function* (s } var failed = false; if (options && options.onRow) { - // Errors in onRow don't stop the query unless StopIteration is thrown - onRow = function (row) { + // Errors in onRow don't stop the query unless the 'cancel' function is called + onRow = function (row, cancel) { try { - options.onRow(row); + options.onRow(row, cancel); } catch (e) { - // If the onRow throws a StopIteration, stop gracefully - if (e instanceof StopIteration) { - Zotero.debug("Query cancelled", 3); - } - // Otherwise, mark the promise as rejected, which Sqlite.jsm doesn't do - // on a StopIteration by default - else { - failed = e; - } - throw StopIteration; + failed = e; + cancel(); } } } diff --git a/components/zotero-autocomplete.js b/components/zotero-autocomplete.js index f41b944baa..85a87bba5c 100644 --- a/components/zotero-autocomplete.js +++ b/components/zotero-autocomplete.js @@ -214,10 +214,11 @@ ZoteroAutoComplete.prototype.startSearch = Zotero.Promise.coroutine(function* (s var onRow = null; // If there's a result callback (e.g., for sorting), don't use a row handler if (!resultsCallback) { - onRow = function (row) { + onRow = function (row, cancel) { if (this._cancelled) { Zotero.debug("Cancelling query"); - throw StopIteration; + cancel(); + return; } var result = row.getResultByIndex(0); var comment = row.getResultByIndex(1); diff --git a/test/tests/dbTest.js b/test/tests/dbTest.js index a83c89afb0..62d5afc7d1 100644 --- a/test/tests/dbTest.js +++ b/test/tests/dbTest.js @@ -151,16 +151,17 @@ describe("Zotero.DB", function() { assert.equal(e.message, "Failed"); }); - it("should stop gracefully if onRow throws a StopIteration", function* () { + it("should stop gracefully if onRow calls cancel()", function* () { var i = 0; var rows = []; yield Zotero.DB.queryAsync( "SELECT * FROM " + tmpTable, false, { - onRow: function (row) { + onRow: function (row, cancel) { if (i > 0) { - throw StopIteration; + cancel(); + return; } rows.push(row.getResultByIndex(0)); i++;