Skip to content

Instantly share code, notes, and snippets.

@arihantdaga
Created April 26, 2022 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arihantdaga/16f8cde6e8fc6c2455a441d7e120cae5 to your computer and use it in GitHub Desktop.
Save arihantdaga/16f8cde6e8fc6c2455a441d7e120cae5 to your computer and use it in GitHub Desktop.
Getting API response with minimum latency
Given an API - /all_data which returns all the schools, all the students and all the books from DB. And return the reponse with minimum possible latency.
And if any of the query fails, returns balnk array for only that entity.
Example -
router.get("/all_data", async (req,res,next)=>{
let schools = await School.find(); // takes 300ms to complete
let students = await Student.find(); // takes 300ms to complete
let books = await Book.find(); // takes 300ms to complete
return res.json({
schools: schools,
students: students,
books: books
});
})
Problem with this code is that this request is taking 900ms right now to respond. Also if any of the query fails, we do not get any data.
We want to handle the erro such that (for example) if books query fails we should be sending -
{
schools: schools,
studenmts: students,
books: []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment