Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
await have to execute chained method one is async(.find) and other is sync (.cahe()) so but cache (second method is executed first) then find?

In overview function we called await Tour.find().cache() so find should be called first then cache but the first acutally cache is executed first then find method ? Is this because await is executing sync and async function it first exeution sync then async (yes without escaping to next line of code ) or it is something else

Here is handler function (Overview.js) exports.getOverview = catchAsync(async (req, res, next) => {

Can you explain why this is not executing the way I think? const tours = await Tour.find().cache(); res.status(200).render("overview", { title: "All Tours", tours, }); });

Here is function defined in exec.js const { exec } = mongoose.Query.prototype;

    mongoose.Query.prototype.cache = function () {
      this.useCache = true;
      return this;
    };
    
    
    mongoose.Query.prototype.exec = async function () {
      
      if (!this.useCache) return exec.apply(this, arguments);
    
      
      const key = JSON.stringify(
        Object.assign({}, this.getQuery(), {
          collection: this.mongooseCollection.name,
        })
      );
    
      
      const cacheValue = await client.get(key);
    
    
      if (cacheValue) {
        const doc = JSON.parse(cacheValue);
        return Array.isArray(doc) ? doc.map((d) => this.model(d)) : this.model(doc);
      }
    
      // if we do, return that
    
      // Otherwise, issue the query and store the result in redis
    
      const result = await exec.apply(this, arguments);
    
      client.set(key, JSON.stringify(result), "Ex", 10);
    
      return result;
    };
    
                    
                   
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!