Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

309
Visualizações
How can I wait for the child to execute a function and perform an $emit to resolve a promise?

I have a button that changes a prop that I am passing to a child.

In the child I am watching for changes in this property and will perform a function to validate some fields. After it I am using $emit to change the state of a variable in my parent.

I tried to return a new Promise with a while function inside that keeps checking for a boolean value every 250ms:

methods: {
endPersonalTabValidation(x) {
  this.waiting= false
  this.accountValidated = x
},
startPersonalTabValidation() {
  this.keyPersonalTab = String(Math.floor(Math.random() * 10))

  this.waiting = true

  return new Promise((resolve, reject) => {
    do {
      setTimeout(() => {
        console.log('Waiting...')
      }, 250)
    } while (this.waiting)

    if (this.accountValidated) {
      resolve(true)
    } else {
      reject()
    }
  })
}...

And then in the child call:

<personal-tab
    :validate-tab="keyPersonalTab"
    @clicked="endPersonalTabValidation"
        />

And the watch:

watch: {
    validateTab(newVal, oldVal) {
      this.validationForm()
      console.log(newVal, oldVal)
    },
  },

The child method:

validationForm() {
  return new Promise((resolve, reject) => {
    this.$refs.validatePersonal.validate().then(success => {
      if (success) {
        this.$emit('clicked', true)
        resolve(true)
      } else {
        this.$emit('clicked', false)
        reject()
      }
    })
  })
},

I want to stop the while in the parent when this.waiting changes to false. Then I can perform the if to resolve or reject:

if (this.accountValidated) {
  resolve(true)
} else {
  reject()
}

This is a next button that will allow the user to advance to the next part of the wizard form.

I am getting a infinite loop.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda