frankcollins3 / PHPokedex

react concepts
0 stars 0 forks source link

code is purposed to open 1 browser, navigate, navigate again [9:26pm] #49

Open frankcollins3 opened 1 year ago

frankcollins3 commented 1 year ago
puppeteer: {
  type: new GraphQLList(GraphQLString),    
  description: 'Invoke Puppeteer',
  resolve: async () => {
    const browser = await puppeteer.launch({headless: false})  
    const page = await browser.newPage();
      const gotoJS = async (url) => {
        if (typeof url === 'string') {
          page.goto(url)
          await page.screenshot({ path: url.slice(url.length - 20, url.length )})              
        } else {
          return {err: 'error'}
        }
      }
      // Open a new page and navigate to youtube.com
      **const getPhotos = async () => {
            await setTimeout( () => {            
              gotoJS('https://6370dd3642049a3b9b369a98--pokedex-of-kanto.netlify.app/')                                               
            }, 1000)
            await setTimeout( () => {
            gotoJS('https://mine-nugget.vercel.app/')                                               
          }, 1000)**
      }
      getPhotos()

      return 'hey good sir' || page || pagenav || "hey theres no way"        
  }
},

authors: {

this code is causing the problems

the problem is that: a new browser tab and window appears, the page navigates to the URL, and without awaiting the second setTimeout()=> ... the code within that 2nd setTimeout() => is executed without await

first proposed approach: 1) use a Promise.all() 2) make use of the puppeteer method .close() -------> as in browser.close()

frankcollins3 commented 1 year ago

first approach I'm going with:

resolve: async () => { const browser = await puppeteer.launch({headless: false})
const page = await browser.newPage();
// move these to be a few lines of code loewr within gotoJS() scope const gotoJS = async (url) => { if (typeof url === 'string') { page.goto(url) await page.screenshot({ path: url.slice(url.length - 20, url.length )})
} else { return {err: 'error'} } }

set puppeteer.launch() && browser.newPage() to be within the scope of gotoJS()

part of this issue is that I thought the callback function that parsed for URL would suffice.

Specifically, page.goto() being invoked again, does not reassign the already declared browser window to a new url It opens up the browser again.

[9:33pm]

frankcollins3 commented 1 year ago
    puppeteer: {
      type: new GraphQLList(GraphQLString),    
      description: 'Invoke Puppeteer',
      resolve: async () => {
        const gotoJS = async (url) => {
            let url_path = url;
            let browserPromise = new Promise( (resolve, reject) => {
              const browser = puppeteer.launch( {headless: false} )
              .then( () => {
                resolve(
                  page = browser.newPage(),
                  page.goto(url || url_path),
                  browser.close()
                )
              })
              .reject( (err) => {
                console.log('oh wow we have an error')
                return err
              })
              browserPromise()              
            })

here is my promise that isn't working.

[9:42pm]