Skip to content

Instantly share code, notes, and snippets.

@abegehr
Last active May 7, 2020 10:32
Show Gist options
  • Save abegehr/d49a7ed74b2c3a44837d189fd885d3bb to your computer and use it in GitHub Desktop.
Save abegehr/d49a7ed74b2c3a44837d189fd885d3bb to your computer and use it in GitHub Desktop.
Fetching data from Firestore simultaneously.
// 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