0

Is this code a Promise ?

 let fetchData = fetch("https://jsonplaceholder.typicode.com/users")
  .then(res => res.json())
  .then(data => {
    console.log(data[0].name)
  })
  .catch(err => {
    console.log('Opps! something went wrong :(')
  })

What is the difference between having a Promise like this and create a new Promise which has resolve and reject to invoke .then and .catch blocks ?

NathanP
  • 49
  • 5
  • Please read. https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Umair Khan Apr 04 '20 at 05:51
  • Also. https://javascript.info/promise-chaining – Umair Khan Apr 04 '20 at 05:53
  • `fetch()` also returns a `Promise` that resolves to the Response to that request, whether it is successful or not. But the Fetch API provides an interface for fetching resources (including across the network). It will seem familiar to anyone who has used XMLHttpRequest, but the new API provides a more powerful and flexible feature set. – palaѕн Apr 04 '20 at 05:54
  • @palaѕн Thank you now i got the point where i missed – NathanP Apr 04 '20 at 06:23

1 Answers1

1

fetch function is returning a Promise object that's why you are able to use .then and .catch blocks.