Skip to content
Advertisement

Typescript: Call-Back Function called much later in the code after await fetch

We are trying to download binary files by using the await fetch as illustrated in the following function. The steps that the following function should be doing are the following:

  1. we receive a URL as argument
  2. we retrieve the Blob
  3. Call-Function to C++

The expected result is illustrated in section 1. The wrong result that we are getting is illustrated in section 2. It’s as if the Call-Back function is being ignored, the fetches are being done as if the call-back function doesn’t exist.

By looking at the following piece of code. Do you see anything wrong in how await and fetch are being done.

We do not want and cannot use XMLHttpRequest.


JavaScript

1. Expected Output:

JavaScript

2. Wrong Ouput:

JavaScript

Advertisement

Answer

If you want to make the downloads run sequentially, you should await them:

JavaScript

You can only use await within a function, though, so you probably want something like this:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement