Created
April 26, 2022 08:10
-
-
Save arihantdaga/16f8cde6e8fc6c2455a441d7e120cae5 to your computer and use it in GitHub Desktop.
Getting API response with minimum latency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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