Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple select query at once in sequelize using single await

Have to run both codes at a single time. Don't want to wait and then fire next query but wait for both query execution complete.

products = await Product.findAll()
.then(data => {
  return data;
})
.catch(error => {
  //
});

variationProducts = await VariationProduct.findAll()
.then(data => {
  return data;
})
.catch(error => {
  //
});
like image 593
Nitin Singh Naruka Avatar asked Nov 29 '25 16:11

Nitin Singh Naruka


2 Answers

You may choose

const [ productsPromise, variationProductsPromise ] = await Promise.all([Product.findAll(), VariationProduct.findAll()]);

OR

const [ productsPromise, variationProductsPromise ] = await { Product.findAll(), VariationProduct.findAll()}
like image 66
Vipul Patil Avatar answered Dec 02 '25 06:12

Vipul Patil


try {
    const [products, variationProducts] = await Promise.all([
        Product.findAll(),
        VariationProduct.findAll()
    ]);

    // Do what you need with the result;
}
catch(e) {
    console.error('Problem in getting data', e);
    throw e; // Or do what you want.
}

like image 36
Naor Levi Avatar answered Dec 02 '25 06:12

Naor Levi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!