0
(() => {fetch('https://jsonplaceholder.typicode.com/posts/1')
            .then((response) =>{
                return new Promise(
                    (resolve) => setTimeout(() => {resolve(response.json())} , 2000)
                )
            })
                .then((d)=>console.log(d))})()

how can I output a promise to the console, and not data

Tell me why, by throwing a promise response.json(), I get its result

  • "*Tell me why, by throwing a promise response.json(), I get its result*" [Why does .json() return a promise?](https://stackoverflow.com/q/37555031) Also, see [What is the explicit promise construction antipattern and how do I avoid it?](https://stackoverflow.com/q/23803743) because you only need `.then((response) => response.json())` instead of using the promise constructor – VLAZ Mar 12 '23 at 19:14
  • @VLAZ OP needs the promise constructor for the `setTimeout`. (Though I don't understand why they use a timeout here) – Bergi Mar 12 '23 at 19:16
  • `.then(d => console.log(d))` will never log a promise to the console, as it is impossible to fulfill a promise with another promise. Sorry. – Bergi Mar 12 '23 at 19:17
  • @Bergi I suspect they don't need the timeout. Seems like a copy/pasted workaround. – VLAZ Mar 12 '23 at 19:17

0 Answers0