Promise cleanup in Zotero.Attachment.importFromURL()

Use `new Zotero.Promise` instead of `defer()` and `coroutine()` instead
of chained promises
This commit is contained in:
Dan Stillman 2017-04-07 23:33:34 -04:00
parent 4540edb622
commit c0a2ec8a47

View file

@ -257,42 +257,43 @@ Zotero.Attachments = new function(){
// Save using a hidden browser
var nativeHandlerImport = function () {
var deferred = Zotero.Promise.defer();
return new Zotero.Promise(function (resolve, reject) {
var browser = Zotero.HTTP.processDocuments(
url,
function() {
Zotero.Promise.coroutine(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+"'"));
reject(new Error("Invalid response " + channel.responseStatus + " "
+ channel.responseStatusText + " for '" + url + "'"));
return;
}
}
return Zotero.Attachments.importFromDocument({
try {
let attachmentItem = yield 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);
});
},
resolve(attachmentItem);
}
catch (e) {
Zotero.logError(e);
reject(e);
}
finally {
Zotero.Browser.deleteHiddenBrowser(browser);
}
}),
undefined,
undefined,
true,
cookieSandbox
);
return deferred.promise;
});
};
// Save using remote web browser persist