commit c0a2ec8a47abbb745b289f52a4ee58570106be99
parent 4540edb622257c6f177d9aebb08216ee85792827
Author: Dan Stillman <dstillman@zotero.org>
Date: Fri, 7 Apr 2017 23:33:34 -0400
Promise cleanup in Zotero.Attachment.importFromURL()
Use `new Zotero.Promise` instead of `defer()` and `coroutine()` instead
of chained promises
Diffstat:
1 file changed, 36 insertions(+), 35 deletions(-)
diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js
@@ -257,42 +257,43 @@ Zotero.Attachments = new function(){
// Save using a hidden browser
var nativeHandlerImport = function () {
- var deferred = Zotero.Promise.defer();
- var browser = Zotero.HTTP.processDocuments(
- url,
- function() {
- let channel = browser.docShell.currentDocumentChannel;
- if (channel && (channel instanceof Components.interfaces.nsIHttpChannel)) {
- if (channel.responseStatus < 200 || channel.responseStatus >= 400) {
- deferred.reject(new Error("Invalid response "+channel.responseStatus+" "+channel.responseStatusText+" for '"+url+"'"));
- return;
+ return new Zotero.Promise(function (resolve, reject) {
+ var browser = Zotero.HTTP.processDocuments(
+ url,
+ Zotero.Promise.coroutine(function* () {
+ let channel = browser.docShell.currentDocumentChannel;
+ if (channel && (channel instanceof Components.interfaces.nsIHttpChannel)) {
+ if (channel.responseStatus < 200 || channel.responseStatus >= 400) {
+ reject(new Error("Invalid response " + channel.responseStatus + " "
+ + channel.responseStatusText + " for '" + url + "'"));
+ return;
+ }
}
- }
- return Zotero.Attachments.importFromDocument({
- libraryID,
- document: browser.contentDocument,
- parentItemID,
- title,
- collections,
- saveOptions
- })
- .then(function (attachmentItem) {
- deferred.resolve(attachmentItem);
- })
- .catch(function(e) {
- Zotero.logError(e);
- deferred.reject();
- })
- .finally(function() {
- Zotero.Browser.deleteHiddenBrowser(browser);
- });
- },
- undefined,
- undefined,
- true,
- cookieSandbox
- );
- return deferred.promise;
+ try {
+ let attachmentItem = yield Zotero.Attachments.importFromDocument({
+ libraryID,
+ document: browser.contentDocument,
+ parentItemID,
+ title,
+ collections,
+ saveOptions
+ });
+ resolve(attachmentItem);
+ }
+ catch (e) {
+ Zotero.logError(e);
+ reject(e);
+ }
+ finally {
+ Zotero.Browser.deleteHiddenBrowser(browser);
+ }
+ }),
+ undefined,
+ undefined,
+ true,
+ cookieSandbox
+ );
+ });
};
// Save using remote web browser persist