Здесь вы можете использовать функцию Promise.all()
для параллельного выполнения всех ваших запросов, а затем ждать, пока все они не будут завершены, например:
exports.getAppStats = (req, res, next) => {
const startToday = moment(req.query.today).startOf('day');
const endToday = moment(req.query.today).endOf('day');
const startsevenDays = moment(req.query.today).subtract(7, 'd').startOf('day');
const startThirtyDays = moment(req.query.today).subtract(30, 'd').startOf('day');
const startYear = moment(req.query.today).subtract(365, 'd').startOf('day');
const stats = {}
// get promises for each query
let today = Application.countDocuments({ dateTime: { $gte: startToday, $lte: endToday } });
let sevenDays = Application.countDocuments({ dateTime: { $gte: startsevenDays, $lte: endToday } });
let thirtyDays = Application.countDocuments({ dateTime: { $gte: startThirtyDays, $lte: endToday } });
let year = Application.countDocuments({ dateTime: { $gte: startYear, $lte: endToday } });
// wait until all promises have been resolved, then set stats & send response
Promise.all([today, sevenDays, thirtyDays, year]).then(([todayData, sevenDaysData, thirtyDaysData, yearData]) => {
stats = {
today: todayData,
sevenDays: sevenDaysData,
thirtyDays: thirtyDaysData,
year: yearData
}
res.status(200).json({data: stats});
}, (err) => {
console.log(err);
// next(err);
// res.status(500).json({ message: "Something went wrong..." });
});
}
Кроме того, вы можете использовать ключевые слова async/await
, чтобы вообще избежать обратных вызовов. Хотя для обработки ошибок потребуется try/catch
. Честно говоря, это не имеет значения, это чисто предпочтение.
// note this is now an async function
exports.getAppStats = async (req, res, next) => {
const startToday = moment(req.query.today).startOf('day');
const endToday = moment(req.query.today).endOf('day');
const startsevenDays = moment(req.query.today).subtract(7, 'd').startOf('day');
const startThirtyDays = moment(req.query.today).subtract(30, 'd').startOf('day');
const startYear = moment(req.query.today).subtract(365, 'd').startOf('day');
// get promises for each query
let today = Application.countDocuments({ dateTime: { $gte: startToday, $lte: endToday } });
let sevenDays = Application.countDocuments({ dateTime: { $gte: startsevenDays, $lte: endToday } });
let thirtyDays = Application.countDocuments({ dateTime: { $gte: startThirtyDays, $lte: endToday } });
let year = Application.countDocuments({ dateTime: { $gte: startYear, $lte: endToday } });
try {
// wait until all promises have been resolved, then set stats & send response
// await Promise.all instead of using callbacks
let [todayData, sevenDaysData, thirtyDaysData, yearData] = await Promise.all(
[today, sevenDays, thirtyDays, year]
);
let stats = {
today: todayData,
sevenDays: sevenDaysData,
thirtyDays: thirtyDaysData,
year: yearData
}
res.status(200).json({data: stats});
} catch(err) {
console.log(err);
// next(err);
// res.status(500).json({ message: "Something went wrong..." });
}
}