如何使用single-await在sequelize中同时运行多选查询



必须同时运行两个代码。不希望等待然后激发下一个查询,而是等待两个查询执行完成。

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

您可以选择

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

OR

const [ productsPromise, variationProductsPromise ] = await { Product.findAll(), VariationProduct.findAll()}
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.
}

你可以这样做:

Promise.all([Product.findAll(), VariationProduct.findAll()]).then(data => {
// data [0] is products
// data [1] is variationProducts 
}).catch(error => {
// oops some error
});
const products = Product.findAll();
const variationProducts = VariationProduct.findAll();
const productsPromise = await products;
const variationProductsPromise = await variationProducts;

最新更新