Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

what's the difference between using then in argument and not

what’s the difference between these two promises one is used in argument other outisde , which one is preferred

fetch(API_URL + "films")
  .then(response => response.json())
  .then(films => {
    output.innerText = getFilmTitles(films);
  })
  .catch(error => output.innerText = ":(")

fetch(API_URL + "films")
  .then(response => 
    response.json()
      .then(films => {
        output.innerText = getFilmTitles(films);
      }))
  .catch(error => output.innerText = ":(")

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is probably opinion based. I think the first one is preferred because you won’t end up with nested promises and should be easier to read.

To make it more obvious:

fetch(API_URL + 'films')
  .then(response => response.json())
  .then(films => {
    output.innerText = getFilmTitles(films);
  })
  .catch(error => output.innerText = ':(');

vs

fetch(API_URL + 'films')
  .then(response => response.json()
    .then(films => {
      output.innerText = getFilmTitles(films);
    })
    .catch(error => output.innerText = ':(')
  );

The number of indentation of the second way would grow while the number of indentations in the first approach is fixed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading