From 04e7c9fca568b5260b215c2750f20dbdf361115f Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 23 Aug 2021 10:53:44 +0400 Subject: [PATCH] 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 --- core/server/services/oembed.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/core/server/services/oembed.js b/core/server/services/oembed.js index 2edeb24109..766359e450 100644 --- a/core/server/services/oembed.js +++ b/core/server/services/oembed.js @@ -155,7 +155,7 @@ class OEmbed { /** * @param {string} _url - * @param {string} cardType + * @param {string} [cardType] * * @returns {Promise} */ @@ -267,22 +267,27 @@ class OEmbed { * @returns {Promise} */ 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); + } } }