0

If I want to display all resources on a page, I might do:

Resource.find({}).exec(function (err, resources) {                          
    res.render("view", {                                                   
        resources: resources

However, what if I want to display all resources and all projects on the page simultaneously? I could do:

Resource.find({}).exec(function (err, resources) {
    Projects.find({}).exec(function (err, projects) {
        res.render("view", {
            resources: resources,
            projects: projects

I think that there has to be a better/more correct way to do this, though.

1 Answer 1

2
var async = require('async');
var resourcesQuery = Resource.find({});
var projectsQuery = Projects.find({});
var resources = {
  resources: resourcesQuery.exec.bind(resourcesQuery),
  projects: projectsQuery.exec.bind(projectsQuery)
};
async.parallel(resources, function (error, results) {
  if (error) {
    res.status(500).send(error);
    return;
  }
  res.render("view", results);
});

This will make the queries in parallel instead of serial, which will probably be faster.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.