T
T
Tash1moto2018-05-14 17:15:22
JavaScript
Tash1moto, 2018-05-14 17:15:22

What is the best way to change the code?

What can be done about it? either do it synchronously or wait like that?
or somehow combine requests into one

let categories,products,seasons,sizes
    categoryModel.find({}).then(a=>{
      categories=a
      productModel.find({}).then(b=>{
        products=b
        seasonModel.find({}).then(c=>{
          seasons=c
          sizeModel.find({}).then(d=>{
            sizes=d
            res.render("admin",{categories,products,seasons,sizes})
          })
        })
      })
    })

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yustas Alexu, 2018-05-14
@Tash1moto

Approximately like this:

const queries = [
    categoryModel.find({}),
    productModel.find({}),
    seasonModel.find({}),
    sizeModel.find({})
];
Promise.all(queries).then(result => {
    console.log(result);
}).catch(err => {
    throw err;
});

0
0xD34F, 2018-05-14
@0xD34F

Promise.all
or await if they must be executed sequentially

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question