Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

303
Vistas
where Should i put async await, on class js or nuxt html?

classWooCommerceProducts.js

import {
    WooComRestApi
} from "~/plugins/classWooCommerceOrigin.js";

export class WooCommerceProducts {
    constructor() {

    }

//-- A method --
async get() {
    const getproducts = await WooComRestApi.get("products").then((response) => {
        return response.data
    }).catch((error) => {
        return "WooCommerceProducts get failre"
    })

    return getproducts
}

//or

//-- B method --    
    get() {
    const getproducts = WooComRestApi.get("products").then((response) => {
        return response.data
    }).catch((error) => {
        return "WooCommerceProducts get failre"
    })

    return getproducts
}
}

products.vue

   // -- A method --
        methods: {
            getProducts() {
                const WooComProducts = new WooCommerceProducts()
                WooComProducts.get().then((response) => {
                    this.products = response
                }).catch((error) => {
                    throw new Error(error)
                })
            }
        },

or
   // -- B method --
        methods: {
            async getProducts() {
                const WooComProducts = new WooCommerceProducts()
                await WooComProducts.get().then((response) => {
                    this.products = response
                }).catch((error) => {
                    throw new Error(error)
                })
            }
        },

guys a quick question on two part

  1. i have a js class is calling from backend and get back products data, both code works, one is i async and await on the html vue the other one is i async await on the js class, so which one did right ? can anyone tell me what different dose it make ?

  2. how do i return the data or the error back to the function getProducts and result same return as the class. if fail return "wooCommerceProducts : get failure" and it will return back to my getProducts catch error functions.

learning more 😄 greatful for sharing your advice. How will you implete if was you ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The thing to remember is that async/await is just more modern syntax for promise .then(). It doesn't make sense to mix them.

how do i return the data or the error back to the function getProducts

Return the data explicitly in the async syntax (inside a try/catch). In the older promise style, return the promise.

//-- A method --
async get() {  // note that no "then" appears here
  try {
    const response = await WooComRestApi.get("products");
    return response.data;  // note the return
  } catch(err) {
    console.log("WooCommerceProducts failure");
    throw err;
  }
}

//or, using the older syntax

//-- B method --    
get() {
  return WooComRestApi.get("products").then((response) => {  // note the return
    return response.data;
  }).catch((error) => {
    console.log("WooCommerceProducts get failure");
    throw error;
  });
}

i have a js class is calling from backend and get back products data, both code works, one is i async and await on the html vue the other one is i async await on the js class, so which one did right ?

Same pattern as before "A method" is correct because it doesn't need a return. For "B method", you've chosen the new style, so stick with it...

// -- B method --
methods: {
  async getProducts() {
    const WooComProducts = new WooCommerceProducts()
    try {
     this.products = await WooComProducts.get();
    } catch(error) {
      // handle error
    }
  }
},
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda