Promise cleanup in Zotero.Attachment.importFromURL()
Use `new Zotero.Promise` instead of `defer()` and `coroutine()` instead of chained promises
This commit is contained in:
parent
4540edb622
commit
c0a2ec8a47
1 changed files with 36 additions and 35 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue