Refactored oembed service to async/await syntax

no issue

- The method was super hard to read with unintuitive catches in multiple places and lots of conditional logic. There's still more to reshuffle here, but that would be for the next time. At least now the data flow is clear within the method
This commit is contained in:
Naz 2021-08-23 10:53:44 +04:00
parent 0703596ace
commit 04e7c9fca5

View File

@ -155,7 +155,7 @@ class OEmbed {
/**
* @param {string} _url
* @param {string} cardType
* @param {string} [cardType]
*
* @returns {Promise<Object>}
*/
@ -267,22 +267,27 @@ class OEmbed {
* @returns {Promise<Object>}
*/
async fetchOembedDataFromUrl(url, type) {
if (type === 'bookmark') {
return this.fetchBookmarkData(url)
.catch(this.errorHandler(url));
}
let data;
return this.fetchOembedData(url).then((response) => {
if (!response && !type) {
try {
if (type === 'bookmark') {
return this.fetchBookmarkData(url);
}
return response;
}).then((response) => {
if (!response) {
return this.unknownProvider(url);
data = await this.fetchOembedData(url);
if (!data && !type) {
data = await this.fetchBookmarkData(url);
}
return response;
}).catch(this.errorHandler(url));
if (!data) {
data = await this.unknownProvider(url);
}
return data;
} catch (e) {
return this.errorHandler(url);
}
}
}