-5
Code:
async function verifyEmailSend() {
try {
if (!document.querySelector("#showAllThreads .pl63")) return false
var coments = [...document.querySelectorAll("#showAllThreads .pl63")]
espereResolver = coments.map(async (element, index) => {
return await new Promise(resolve => {
setTimeout(() => {
resolve(element.querySelector('div > h1 > div i').getAttribute('title'))
}, index * 200)
})
})
resp = await Promise.all(espereResolver)
if (resp.find(element => element == 'Enviado ( e-mail )')) {
return true
} else {
return false
}
} catch (error) {
//console.log(error)
}
}
async function verifyAgente() {
return new Promise(resolve => {
setTimeout(() => {
if (!document.querySelector("#assigneeBtnContainer > div.fleft")) return resolve({ status: false })
nome = document.querySelector("#assigneeBtnContainer > div.fleft").innerText
return resolve({ nomeAgente: nome, status: true })
}, 700)
})
}
async function sendEmail(text) {
try {
return new Promise(resolve =>{
document.querySelector("#ticket-replyoptions").click()
setTimeout(() => {
document.querySelector("#replyContainer div > iframe").contentWindow.document.querySelector("body > div:nth-child(1)").innerText = text
resolve()
}, 1500)
})
} catch (error) {
//console.log(error)
}
}
async function desatribuirChamado(){
return new Promise(resolve =>{
setTimeout(()=>{
document.querySelector("#assigneeBtnContainer > div.fleft").click()
setTimeout(()=>{
document.querySelector("#smt_agent_teams_div > div.mark_assi").click()
resolve()
},1500)
}, 1500)
})
}
async function start(text) {
chamados = [...document.querySelectorAll(".listItem > div > div.subj_id.fleft.pointer")]
if(!chamados) return //console.log('Acabou os chamados!')
promises = await chamados.map(async (element, index) => {
return new Promise( resolve =>{
setTimeout(() => {
element.click()
agente = await verifyAgente()
emailEnviado = await verifyEmailSend()
if(agente.status){
//console.log(`${agente.nomeAgente} já está atendendo este cliente`)
return resolve()}
if(emailEnviado){
//console.log('Já foi enviado e-mail a este cliente!')
return resolve()
}
await sendEmail(text)
await desatribuirChamado()
resolve()
}, index * 5000)
})
})
await Promise.all(promises)
return start(text)
}
text = '.'
await start(text)
Can anyone tell me why the error returns to me: Uncaught Syntaxerror: await is only Valid in async functions and the top level bodies of modules
Tell me the mistake is here:
agente = await verifyAgente()
But Function start e is async!
To proceed with your question it is important you [Edit] the question and exchange the code for a [mcve] of the problem. To understand what kind of question serves the site and consequently avoid closures worth reading What is the Stack Overflow and the Stack Overflow Survival Guide (summarized) in Portuguese.
– Bacco