Skip to content

Instantly share code, notes, and snippets.

@CodingDoug
Last active November 6, 2022 09:29
Show Gist options
  • Select an option

  • Save CodingDoug/814a75ff55d5a3f951f8a7df3979636a to your computer and use it in GitHub Desktop.

Select an option

Save CodingDoug/814a75ff55d5a3f951f8a7df3979636a to your computer and use it in GitHub Desktop.

Revisions

  1. CodingDoug revised this gist Aug 7, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # 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](https://img.youtube.com/vi/Jr7pDZ1RAUg/0.jpg)](https://www.youtube.com/watch?v=Jr7pDZ1RAUg)
  2. CodingDoug revised this gist Jun 7, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    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](https://img.youtube.com/vi/Jr7pDZ1RAUg/0.jpg)](https://www.youtube.com/watch?v=Jr7pDZ1RAUg)

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

    ```text
  3. CodingDoug created this gist Jun 6, 2018.
    19 changes: 19 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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.

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

    ```text
    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.
    ```
    12 changes: 12 additions & 0 deletions example1-1-before.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    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)
    })
    })
    12 changes: 12 additions & 0 deletions example1-2-after.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    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)
    }
    })
    25 changes: 25 additions & 0 deletions example2-1-before.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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)
    })
    })
    26 changes: 26 additions & 0 deletions example2-2-after.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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)
    }
    })