I have seen many youtube videos/courses/"books" on promises with fetch, but they didn't help. This is just confusing.
I understand this bit:
fetch("some url/file") // get "data" from some API and it returns a promise
.then( res => res.json()) // this is a promise but not the raw data that we need
.then(data => console.log(data) // with this I will actually access the actual data
.catch( err => console.log(err) // this is if errors exists but ONLY if It can't open the actual site, like their server is dead or whatever, I know that with an IF statements I can check if the status is 200 and that can work with errors.
I was reading more about it and a site said "The new Promise() constructor should only be used for legacy async tasks, like usage of setTimeout or XMLHttpRequest. A new Promise is created with the new keyword and the promise provides resolve and reject functions to the provided callback:"" ,but then what about resolve and reject? Do I even need them?
Then, I read on stackoverflow that if you are fetching, you don't need to make a "new Promise" because fetch already returns a promise(though in the stackoverflow example the person was writing it like this, in a variable"
function get(url) {
console.log('Making fetch() request to: ' + url);
let promise = new Promise((resolve, reject) => {
fetch(url).then // etc etc
")
So the main questions are:
What about "new Promise"? What is it? Do I need it? When? Talk to me about it.
If I need "new Promise", do I put it into a variable (let test = new Promise) or do I return it in a function (function test(){ return new Promise}) //etc etc, what is the difference?
If I need "new Promise", when do I resolve and reject? WHAT do I resolve/reject?
If you want to answer these questions with an in depth link, explaining it, feel free.
Thank you for your time.