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
})
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
})
})();