Code in Browser async/await

Asked

Viewed 25 times

-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!

1 answer

-2


As you said, Function start is async, however, the command agente = await verifyAgente() is within setTimeout which is not an asynchronous function, to solve you can do this:

    promises = await chamados.map(async (element, index) => {
    return new Promise( resolve =>{
        // Dessa forma, a arrow function que está dentro setTimeout será async
        setTimeout(async() => {
            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)
    })

Thus, the Arrow Function that is within setTimeout will be async.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.