0

I required all the models and wanted to send all the static data from the database(Mongo) to a single endpoint /getall API. This is the controller file of the route I need to optimise. It's running fine in Postman but there is a delay. Is there any way to fetch the data faster like without using async and await ?

allController.js

const initiative = require("../Models/initiativeModel");
const event = require("../Models/eventSchema");
const collaborator = require("../Models/collaboratorModel");
const speaker = require("../Models/speakerModel");
const chairman = require("../Models/chairmanModel");
const management = require("../Models/ecellHeadsModel");
const faqs = require("../Models/faqModel");
const startup = require("../Models/startupModel");



exports.getAll = async(req, res,next) =>{
    const initiativesPromise = initiative.find();
    const eventsPromise =  event.find()
    const collabPromise =  collaborator.find()
    const speakPromise =  speaker.find()
    const chairPromise =  chairman.find()
    const managePromise =  management.find()
    const faqPromise =  faqs.find()
    const startupsPromise =  startup.find()

    const initiatives = await initiativesPromise.exec()
    const events =      await eventsPromise.exec()
    const collab =      await  collabPromise.exec()
    const speak =       await speakPromise.exec()
    const chair =       await chairPromise.exec()
    const manage =      await managePromise.exec()
    const faq =         await faqPromise.exec()
    const startups =    await startupsPromise.exec()

    return res.status(200).json({
                                        "events": events,
                                        "initiatives": initiatives,
                                        "collaborators": collab,
                                        "speakers": speak,
                                        "chairman": chair,
                                        "management": manage,
                                        "faq": faq,
                                        "startups": startups
    })

> can Promise.all be a faster way than async and await?

    
}
Pranab Das
  • 69
  • 1
  • 7
  • 2
    You allready awnsered your own question, use `await Promise.all([...])`. See @Liam answer. there is exactly the same solution provided. – Marc Jan 11 '22 at 12:17
  • Thanks .I wasn't sure about the delay happening. I figured it out. – Pranab Das Jan 12 '22 at 20:42

0 Answers0