Skip to content

Instantly share code, notes, and snippets.

@jesselima
Forked from CodingDoug/README.md
Created December 10, 2018 10:56
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 jesselima/2450704c06d670c41f2a471065cb68f6 to your computer and use it in GitHub Desktop.
Save jesselima/2450704c06d670c41f2a471065cb68f6 to your computer and use it in GitHub Desktop.
Example code from the video "Use async/await with TypeScript in Cloud Functions"

Example code from the video "Use async/await with TypeScript in Cloud Functions"

This is the example code from my video about using async/await with Cloud Functions. I've placed it here in a gist so it's easier to compare the "before" and "after" states for each case.

Watch the video here

The code in this project is licensed under the Apache License 2.0.

Copyright 2018 Google LLC
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    https://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
export const getBostonWeather = functions.https.onRequest((request, response) => {
admin.firestore().doc("cities-weather/boston-ma-us").get()
.then(snapshot => {
const data = snapshot.data()
response.send(data)
})
.catch(error => {
// Handle the error
console.log(error)
response.status(500).send(error)
})
})
export const getBostonWeather = functions.https.onRequest(async (request, response) => {
try {
const snapshot = await admin.firestore().doc("cities-weather/boston-ma-us").get()
const data = snapshot.data()
response.send(data)
}
catch (error) {
// Handle the error
console.log(error)
response.status(500).send(error)
}
})
export const getBostonAreaWeather = functions.https.onRequest((request, response) => {
admin.firestore().doc("areas/greater-boston").get()
.then(areaSnapshot => {
const cities = areaSnapshot.data().cities
const promises = []
cities.forEach(city => {
const p = admin.firestore().doc(`cities-weather/${city}`).get()
promises.push(p)
})
return Promise.all(promises)
})
.then(snapshots => {
const results = []
snapshots.forEach(snap => {
const data = snap.data()
data.city = snap.id
results.push(data)
})
response.send(results)
})
.catch(error => {
console.log(error)
response.status(500).send(error)
})
})
export const getBostonAreaWeather = functions.https.onRequest(async (request, response) => {
try {
const areaSnapshot = await admin.firestore().doc("areas/greater-boston").get()
const cities = areaSnapshot.data().cities
const promises = []
cities.forEach(city => {
const p = admin.firestore().doc(`cities-weather/${city}`).get()
promises.push(p)
})
const snapshots = await Promise.all(promises)
const results = []
snapshots.forEach(snap => {
const data = snap.data()
data.city = snap.id
results.push(data)
})
response.send(results)
}
catch (error) {
console.error(error)
response.status(500).send(error)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment