Last active
May 7, 2020 10:32
-
-
Save abegehr/d49a7ed74b2c3a44837d189fd885d3bb to your computer and use it in GitHub Desktop.
Fetching data from Firestore simultaneously.
This file contains hidden or 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
// https://firebase.google.com/docs/firestore/quickstart | |
var db = firebase.firestore(); | |
async function fetchArticle(articleId, authorId) { | |
// get author and article docs simultaneously | |
const authorPromise = db.doc("authors/" + authorId).get(); | |
const articlePromise = db.doc("articles/" + articleId).get(); | |
// wait until both compleeted | |
try { | |
const [authorSnap, articleSnap] = await Promise.all([authorPromise, articlePromise]); | |
} | |
catch (err) {/* handle error */} | |
// extract data | |
const authorData = authorSnap.data(); | |
const articleData = articleSnap.data(); | |
// save to state | |
this.setState({authorData, articleData}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment