-1

Suppose if I want to get a JSON data from a HTTP GET request using observable:

this.getJSON().subscribe(response => {

      for (let item of response) {
        // ... iterate array
      }
});

My question is about how the response is received and handled by observable.

The response data has an Array of say, 1000 elements. Does the HTTP request return JSON data in chunks? say, 100 arrays, then 400 arrays, and then 1000 arrays? Or, before the .subscribe method is called all 1000 array must be fetched?

So, if it is received in chunks then it means the .subscribe method will be called over and over. In my case according to what I tested, it waits until all 1000 array items are fetched before calling subscribe.

I am trying to understand if that's the normal behavior or not.

3
  • JSON is not suitable for chunked streaming. Meaning, typically it has to be loaded entirely before it can be used (parsed). But of course we can't possibly know what this mysterious endpoint does. Unfortunately I can't help with the angular side.
    – freakish
    Commented Feb 12, 2023 at 9:15
  • In general. does HTTP GET returns a partial response which builds over time with chunks of data or does it return only a complete response with data?
    – user963241
    Commented Feb 12, 2023 at 9:30
  • Every network communication may and will transfer data partially. It is up to the other peer to gather the data and combine it. This is typically done behind the scenes by browser. Note however, that HTTP additionally has a mechanism known as chunked transfer encoding which is supported by browsers, and the browser allows you to run code chunk-by-chunk. And so yes, it is possible to run code chunk-by-chunk, if HTTP GET returns Transfer-Encoding: chunked header. It all boils down to how your HTTP endpoint works, and how angular works.
    – freakish
    Commented Feb 12, 2023 at 9:36

1 Answer 1

0

HTTP request does not return in chunks unless you declare it to return that way. Therefore subscribe is called only when the data is completely returned.

subscribe function, is only a nice way to implement callback function.

1
  • Do you mean that server at back-end needs to return in the chunks or, Angular can declare it using observable?
    – user963241
    Commented Feb 12, 2023 at 11:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.