frankcollins3 / PHPokedex

react concepts
0 stars 0 forks source link

Promise seems to need await to work correctly. [4:08pm] #47

Open frankcollins3 opened 1 year ago

frankcollins3 commented 1 year ago
 const check = async () => {
  new Promise( (resolve, reject) => {
      fetch(`http://localhost:5000/pokemon?query={puppeteer}`)
      .then(async(data) => {
        console.log('data in the promise');
        **data = await data.json()**
        data ? 
        resolve( 
            console.log('data'),
            console.log(data)
          )
        : 
        reject("ben and jerry")
      })

    })
}

without the await data.json() , the promise returns a promise that hasn't been fulfilled:

if the await keyword precedes the data.json(), and the scope to which that code belongs is an async function, the data from the GraphQL route is returned successfully: ![Screen Shot 2023-05-13 at 4 08 05 PM](https://github.com/frankcollins3/PHPokedex/assets/73137934/0f505d86-f70c-41f9-86da-4079b7e8bee5) ![Screen Shot 2023-05-13 at 4 07 57 PM](https://github.com/frankcollins3/PHPokedex/assets/73137934/d66ab7f6-25ed-489e-a790-2dc8131cfeec) The problem is that I thought that Promise solves for the awaited data not populating the values before sequential steps are executed. **first proposed approach:** Promise.all?() or leave this how is it is now with the await keyword