From 7b17dc8bfdd1d0e07caa3e8abc9d1b4800d867ba Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 11 Jun 2021 17:12:55 +0200 Subject: [PATCH] Re-factor the `fetchData` helper function, in `src/display/display_utils.js` to be asynchronous --- src/display/display_utils.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/display/display_utils.js b/src/display/display_utils.js index 98b7424b2..95e9180ae 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -46,23 +46,18 @@ class DOMCanvasFactory extends BaseCanvasFactory { } } -function fetchData(url, asTypedArray) { +async function fetchData(url, asTypedArray = false) { if ( (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) || (isFetchSupported() && isValidFetchUrl(url, document.baseURI)) ) { - return fetch(url).then(async response => { - if (!response.ok) { - throw new Error(response.statusText); - } - let data; - if (asTypedArray) { - data = new Uint8Array(await response.arrayBuffer()); - } else { - data = stringToBytes(await response.text()); - } - return data; - }); + const response = await fetch(url); + if (!response.ok) { + throw new Error(response.statusText); + } + return asTypedArray + ? new Uint8Array(await response.arrayBuffer()) + : stringToBytes(await response.text()); } // The Fetch API is not supported.