Question about "response" in asyncronous functions

So I have read bot the asyncronous functions and fetch() articles. In both we can see functions like this:

fetch('products.json')
  .then( response => {
    if (!response.ok) {
      throw new Error(`HTTP error: ${response.status}`);
    }
    return response.json();
  })
  .then( json => initialize(json) )
  .catch( err => console.error(`Fetch problem: ${err.message}`) );

My question is, what is this response word in line 2 or json in line 8 (I know what json is, but why is the term thrown in there)? Is this an arbitrary term? Where does it come from and what is it’s purpose?

I did the task for the asyncronous funtions chapter, completely removed the word “response”, used and empty () instead, and the app continued working (codepen here)

Can you clarify this for me please?

Hi @robmdnnyc

fetch() and json() return a promise. When these promises are fulfilled you receive the result as an argument inside the then() method. For example the promise of the fetch() function resolves to the Response object. So it makes sense to name the argument which receives this data response. Likewise for json which receives a JSON string from response.json().
You could indeed name this arguments however you want, but I think we agree that it’s logical to name them based on their content.

The reason we you could leave the argument out on your linked example: We are only interested to know when the animation is finished (when the promise is fulfilled). We don’t care about what is returned.

Does that make it clearer? Feel free to ask more questions if needed. :slightly_smiling_face:

Have a nice day,
Michael

2 Likes