From ce3f5ea2bf73ab9d4a3d86bc61576f5d14785ab0 Mon Sep 17 00:00:00 2001
From: Jonas Jenwald <jonas.jenwald@gmail.com>
Date: Sun, 29 Aug 2021 12:11:36 +0200
Subject: [PATCH] Use `async` a bit more in the API

This patch changes the `PDFDocumentLoadingTask.destroy`-method and the `_fetchDocument`-function to be `async`, which slightly simplifies the relevant code.

Furthermore, remove the catch-handler from the `WorkerTransport.getPageIndex`-method since it's no longer needed. Given that the `MessageHandler` is nowadays wrapping every possible Exception, it's no longer necessary to try and re-wrap the reason here.
---
 src/display/api.js | 55 ++++++++++++++++++++--------------------------
 1 file changed, 24 insertions(+), 31 deletions(-)

diff --git a/src/display/api.js b/src/display/api.js
index 865c3bb54..81cde0712 100644
--- a/src/display/api.js
+++ b/src/display/api.js
@@ -463,13 +463,13 @@ function getDocument(src) {
  * @param {Object} source
  * @param {PDFDataRangeTransport} pdfDataRangeTransport
  * @param {string} docId - Unique document ID, used in `MessageHandler`.
- * @returns {Promise} A promise that is resolved when the worker ID of the
- *   `MessageHandler` is known.
+ * @returns {Promise<string>} A promise that is resolved when the worker ID of
+ *   the `MessageHandler` is known.
  * @private
  */
-function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
+async function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
   if (worker.destroyed) {
-    return Promise.reject(new Error("Worker was destroyed"));
+    throw new Error("Worker was destroyed");
   }
 
   if (pdfDataRangeTransport) {
@@ -479,8 +479,9 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
     source.contentDispositionFilename =
       pdfDataRangeTransport.contentDispositionFilename;
   }
-  return worker.messageHandler
-    .sendWithPromise("GetDocRequest", {
+  const workerId = await worker.messageHandler.sendWithPromise(
+    "GetDocRequest",
+    {
       docId,
       apiVersion:
         typeof PDFJSDev !== "undefined" && !PDFJSDev.test("TESTING")
@@ -508,13 +509,13 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
       standardFontDataUrl: source.useWorkerFetch
         ? source.standardFontDataUrl
         : null,
-    })
-    .then(function (workerId) {
-      if (worker.destroyed) {
-        throw new Error("Worker was destroyed");
-      }
-      return workerId;
-    });
+    }
+  );
+
+  if (worker.destroyed) {
+    throw new Error("Worker was destroyed");
+  }
+  return workerId;
 }
 
 /**
@@ -587,19 +588,15 @@ class PDFDocumentLoadingTask {
    * @returns {Promise<void>} A promise that is resolved when destruction is
    *   completed.
    */
-  destroy() {
+  async destroy() {
     this.destroyed = true;
+    await this._transport?.destroy();
 
-    const transportDestroyed = !this._transport
-      ? Promise.resolve()
-      : this._transport.destroy();
-    return transportDestroyed.then(() => {
-      this._transport = null;
-      if (this._worker) {
-        this._worker.destroy();
-        this._worker = null;
-      }
-    });
+    this._transport = null;
+    if (this._worker) {
+      this._worker.destroy();
+      this._worker = null;
+    }
   }
 }
 
@@ -2898,13 +2895,9 @@ class WorkerTransport {
   }
 
   getPageIndex(ref) {
-    return this.messageHandler
-      .sendWithPromise("GetPageIndex", {
-        ref,
-      })
-      .catch(function (reason) {
-        return Promise.reject(new Error(reason));
-      });
+    return this.messageHandler.sendWithPromise("GetPageIndex", {
+      ref,
+    });
   }
 
   getAnnotations(pageIndex, intent) {