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

287
Visualizações
Wait for a submit event to finish before calling async function

I want to run an async function after a form has completed its submit. The form updates a shopping cart, and I need to grab the products from the cart after they've been added.

I've tried a million things and nothing seems to wait until the form has COMPLETED submitting before running my code. I don't want to use any sort of timing events.

Here is the latest couple versions of code I have. They both do the same thing and run prior to the submit being finished:

window.addEventListener(
  'submit', 
  () => vueFreshie.$options.components['fresh-credit'].options.methods.getCart(), 
  false
)

window.addEventListener("submit", async e => {
  await vueFreshie.$options.components['fresh-credit'].options.methods.getCart()
}, false)

this is the Vue method I'm calling, and I've simplified it for examples sake.

  methods: {
    async getCart() {
      let response = await axios.get(`/cart.js`)
      debugger
    }
  }

the method is called fine, its just too early and the cart has not been updated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here you go: I've used a onSubmit event handler to listen to the form submission & axios to explicitly post the data to my server. This way I have more control over the submission request and I can await for its completion before proceeding to the getCart API.

new Vue({
  el: "#app",
  data: () => {
    return {
      formData: {
        firstName: '',
        lastName: ''
      }
    }
  },
  methods: {
    async onSubmit() {
      const {data} = await axios.post('https://jsonplaceholder.typicode.com/posts', this.formData);
      console.log('response from submitted request', data);
      await this.getCart();
    },

    async getCart() {
      console.log('safe to call the get cart API now');
      const {data} = await axios.get('https://jsonplaceholder.typicode.com/posts');
      console.log('response from cart api', data);
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<div id="app">
  <form v-on:submit.prevent="onSubmit">
    <input name="firstName" v-model="formData.firstName"><br/>
    <input name="lastName" v-model="formData.lastName">
    <button type="submit">SUBMIT</button>
  </form>
</div>

Reference
  • Event handling in Vue
about 4 years ago · Juan Pablo Isaza Relatório
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