0

I have a problem in Nodejs, the sessionData variable gets its value from a database, in the case that this value exists new client must be executed, the problem I have is that when I run all the code first to run is new client and then get the data from sessionData, how can I do to first get the value of sessionData and then run new client

let sessionData

(async() => { 
sessionData = await cargarSession()
console.log('Mi objeto recibido', sessionData)

//return sessionData 

})();

const wa = new Client({
restartOnAuthFail: true,
puppeteer: {
    headless: true,
    args: [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-dev-shm-usage',
        '--disable-accelerated-2d-canvas',
        '--no-first-run',
        '--no-zygote',
        '--single-process', 
        '--disable-gpu',
        '--use-gl=egl'
    ],
}, 
session: sessionData

})

  • Simply move all that `new Client` code inside the async function, after the `await`, where the `sessionData` is available. – Bergi Sep 19 '21 at 00:55
  • yes I already tried, what happens is that below I have a function that needs wa but if I insert it inside the first function the scope does not allow that function to access the values of wa. – Tommy Solano Peñafiel Sep 19 '21 at 00:57
  • Then move *all* of that code inside the async function. – Bergi Sep 19 '21 at 00:58
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – cbr Sep 19 '21 at 01:12

1 Answers1

0

All you would need to do is move the client creation into the async function.

You also mentioned in the comments that you attempted that however the rest of the script was not able to access the wa object later in the script. To fix that just define wa outside of the async function and reassign it's value.

let wa;

(async() => { 
let sessionData = await cargarSession()
console.log('Mi objeto recibido', sessionData)

wa = new Client({
restartOnAuthFail: true,
puppeteer: {
    headless: true,
    args: [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-dev-shm-usage',
        '--disable-accelerated-2d-canvas',
        '--no-first-run',
        '--no-zygote',
        '--single-process', 
        '--disable-gpu',
        '--use-gl=egl'
    ],
}, 
session: sessionData
})
})();


astro spl
  • 1
  • 1