|
27 | 27 | const app = initializeApp(firebaseConfig);
|
28 | 28 | const functions = getFunctions(app);
|
29 | 29 | connectFunctionsEmulator(functions, "127.0.0.1", 5001);
|
30 |
| - const getForecast = httpsCallable(functions, "getForecast"); |
| 30 | + |
31 | 31 |
|
32 | 32 | const favoriteLocations = [
|
33 | 33 | // Google HQ
|
|
36 | 36 | { latitude: 37.745192257741984, longitude: -119.5945133017153 },
|
37 | 37 | // Old Faithful
|
38 | 38 | { latitude: 44.46037818049411, longitude: -110.82802255265777 },
|
39 |
| - ] |
| 39 | + ]; |
40 | 40 |
|
41 | 41 | async function handleClick() {
|
42 | 42 | // reset result
|
43 | 43 | clearUi();
|
44 | 44 |
|
45 |
| - const resp = await getForecast.stream({ |
| 45 | + // [START stream_data_client] |
| 46 | + // Get the callable by passing an initialized functions SDK. |
| 47 | + const getForecast = httpsCallable(functions, "getForecast"); |
| 48 | + |
| 49 | + // Call the function with the `.stream()` method to start streaming. |
| 50 | + const {stream, data} = await getForecast.stream({ |
46 | 51 | locations: favoriteLocations,
|
47 | 52 | });
|
48 | 53 |
|
49 |
| - // loop through the `stream` async iterable |
50 |
| - for await (const forecastData of resp.stream) { |
51 |
| - updateUi(forecastData); |
| 54 | + // The `stream` async iterable returned by `.stream()` |
| 55 | + // will yield a new value every time the callable |
| 56 | + // function calls `sendChunk()`. |
| 57 | + for await (const forecastDataChunk of stream) { |
| 58 | + // update the UI every time a new chunk is received |
| 59 | + // from the callable function |
| 60 | + updateUi(forecastDataChunk); |
52 | 61 | }
|
| 62 | + |
| 63 | + // The `data` promise resolves when the callable |
| 64 | + // function completes. |
| 65 | + const allWeatherForecasts = await data; |
| 66 | + finalizeUi(allWeatherForecasts); |
| 67 | + // [END stream_data_client] |
53 | 68 | }
|
54 | 69 |
|
55 | 70 | function clearUi() {
|
|
0 commit comments