Stream Promise Results from Server to Client

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional NextJS Lessons

1.Course Intro
free
⏱️ 2:05
2.Get Setup
free
⏱️ 2:05
3.Project Template
free
⏱️ 5:45
4.Introducing FullStack React
free
⏱️ 4:57
5.App Router Reference
free
⏱️ 7:08
6.NextJS Server vs. "use client" Rendering
⏱️ 5:01
7.Nesting Server and Client Components
⏱️ 8:51
8.Client Only Components
⏱️ 3:48
9.Global CSS in NextJS
⏱️ 6:30
10.CSS Modules and SASS
⏱️ 4:15
11.Tailwind CSS
⏱️ 5:17
12.NextJS Custom Fonts
⏱️ 4:08
13.Static vs Dynamic Rendering
⏱️ 4:55
14.NextJS Link Component
⏱️ 3:20
15.NextJS Link Prefetching
⏱️ 5:49
16.Navigation Scroll with Link
⏱️ 2:02
17.Replace Navigation History
⏱️ 1:35
18.NextJS Image Component
⏱️ 7:01
19.Dynamic Image URLs
⏱️ 5:34
20.Safe Colocation with Private Folders
⏱️ 2:12
21.NextJS Route Groups
⏱️ 3:07
22.Dynamic Routes
⏱️ 5:37
23.NextJS API Routes
⏱️ 6:03
24.NextRequest and NextResponse
⏱️ 2:41
25.Cookies in API Handlers
⏱️ 4:25
26.React Suspense Simplified
⏱️ 2:23
27.Power of Suspense Boundaries
⏱️ 3:52
28.use Promises
⏱️ 3:24
29.Stream Promise Results from Server to Client
⏱️ 3:03
30.use Requires a Cached Promise
free
⏱️ 2:30
31.Suspense Error Boundaries
⏱️ 4:42
32.React Server Functions
⏱️ 4:50
33.Server Actions for Powerful Forms
⏱️ 5:35
34.useActionState to Simplify Form Actions
⏱️ 6:38
35.React Serializable Types
⏱️ 2:42
36.Create Dynamic Images with NextJS ImageResponse
⏱️ 5:54
37.Deploy NextJS Application to Production
⏱️ 3:51
38.NextJS Environment Variables Masterclass
⏱️ 3:50
39.Deploying and Managing Environment Variables
⏱️ 4:38
40.Using Databases for Dynamic Server Side State
free
⏱️ 7:34
41.Mastering NextJS Layout Files
⏱️ 4:54
42.Client State Management with NextJS
⏱️ 3:48
43.Client State Management In Action
⏱️ 8:04
44.Using Client State Management Libraries
⏱️ 5:25
45.Detecting Server vs Client Runtime and useEffect
⏱️ 4:33
46.Demystifying ISR vs SSR vs SSG
⏱️ 3:07
47.Static Site Generation or SSG with NextJS
⏱️ 3:16
48.Incremental Static Regeneration (ISR) Simplified
⏱️ 2:51
49.API Routes Caching with NextJS
⏱️ 3:05
50.Reference Guide to Caching for Modern NextJS
⏱️ 2:47
51.Setup a BASE_URL for client and server
free
⏱️ 2:54
52.Creating a sitemap.xml
⏱️ 3:29
53.Provide metadata with NextJS
⏱️ 6:54
54.Created High Quality Social Media Previews
⏱️ 7:38

Stream Promise Results from Server to Client

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Core Concept

We’ve seen how the use api can be used to await a promise in a React component.

But whats even more amazing about this little use function is that it can seamless stream the result of a promise from the server to the client browser!

This has a few advantages:

  • We no longer need to await the promise on the server (as that would block rendering)!
  • We can get the result of API calls made on the server to the client browser!

Example

The in the below example we make a call to fetchLesson from the server (this API is only accessible to server code), and stream the result of to the client (by utilizing use).

app/page.tsx
lib/Lesson/TableOfContents.tsx
lib/Lesson/LessonVideo.tsx
lib/Lesson/LessonDescription.tsx
import { Column, PageRoot, Row } from "@/lib/Layout";
import { PageLoading } from "@/lib/Loaders/PageLoading";
import { Nav } from "@/lib/Nav";
import { TableOfContents } from "@/lib/Lesson/TableOfContents";
import { Suspense } from "react";
import { LessonVideo } from "@/lib/Lesson/LessonVideo";
import { LessonDescription } from "@/lib/Lesson/LessonDescription";
import { fetchLesson } from "@/lib/api";

export default async function Home() {
const lesson = fetchLesson();
return (
<PageRoot>
<Nav />
<Row flex={1}>
<Suspense fallback={<PageLoading />}>
<TableOfContents lesson={lesson} />
<Column flex={1}>
<LessonVideo lesson={lesson} />
<LessonDescription lesson={lesson} />
</Column>
</Suspense>
</Row>
</PageRoot>
);
}
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

We've seen how the use API can be used to evade a promise in a react component. But what's even more amazing about the stable function is that it can seamlessly stream the result of a promise from the server to the client browser. Consider a sample page for a lesson video. It has the NAV one top followed by three dynamic sections, which are the table of contents, the lesson video, and the lesson description. These dynamic sections depend upon an API fetch lesson, which loads the lesson details. This API is not exposed to the public, which means that a call to this particular function can only

00:34

be made from the server. We make the call on the server await the response before rendering the ui. Unfortunately, this means that the user will not see any user interface while this API is spending, and we can demonstrate it within the browser. If you visit this page, the user is left hanging till that API resolves, and then we see the final fully rendered ui. One solution to this problem would be to move the fetch calls within the individual components, but there are two issues with that approach. First, we would end up with three fetch calls instead of one.

01:06

And secondly, these calls cannot even be made from the clients as we've mentioned before. So your mission should choose to accept it, is to find a way to make the fetch call only once on the server and then stream the results to the individual client components. This problem can be solved with the use API instead of awaiting the promise on the server. We make the API call from the server as it can be only done on the server, but we no longer await the result. And instead of passing in the resolved values to the components, we pass the promise to the individual components.

01:39

Right now we are getting errors because the components are expecting the resolved values. But let's modify that. We changed the props of the individual component to accept a promise to the final lesson and then utilize the use API that is built into react that we have looked at before to get the resolve value when the promise will fulfill. Here we've modified the table of contents component to use this new pattern. Let's make the same changes for the other components, which is the lesson video. We accept the promise and then await the resolution with the use API. And finally, within the lesson description,

02:12

we make the same change. We modify the prop to be a promise and then again wait for the final resolve value using use. With our components modified. If we jump back to the homepage, you can see that the errors have gone away. Now while these components are waiting for that promise to resolve, we would like to show a fallback UI to the users. We already know how to achieve that quite easily. Using suspense, we wrap up the dynamic portions with the suspense boundary, providing a fallback user interface. And now when we visit the application within the browser, the fetch lesson request will be made on the server.

02:47

But since we are not waiting for it, we will get back anything that is not dynamic. For example, the nav, the dynamic portions that are pending on that promise are wrapped by the suspense, which is why we see the loader. And eventually, once that promise resolves, the client components are free to render. I.

Professional NextJS

Professional NextJS

1.Course Intro
free
⏱️ 2:05
2.Get Setup
free
⏱️ 2:05
3.Project Template
free
⏱️ 5:45
4.Introducing FullStack React
free
⏱️ 4:57
5.App Router Reference
free
⏱️ 7:08
6.NextJS Server vs. "use client" Rendering
⏱️ 5:01
7.Nesting Server and Client Components
⏱️ 8:51
8.Client Only Components
⏱️ 3:48
9.Global CSS in NextJS
⏱️ 6:30
10.CSS Modules and SASS
⏱️ 4:15
11.Tailwind CSS
⏱️ 5:17
12.NextJS Custom Fonts
⏱️ 4:08
13.Static vs Dynamic Rendering
⏱️ 4:55
14.NextJS Link Component
⏱️ 3:20
15.NextJS Link Prefetching
⏱️ 5:49
16.Navigation Scroll with Link
⏱️ 2:02
17.Replace Navigation History
⏱️ 1:35
18.NextJS Image Component
⏱️ 7:01
19.Dynamic Image URLs
⏱️ 5:34
20.Safe Colocation with Private Folders
⏱️ 2:12
21.NextJS Route Groups
⏱️ 3:07
22.Dynamic Routes
⏱️ 5:37
23.NextJS API Routes
⏱️ 6:03
24.NextRequest and NextResponse
⏱️ 2:41
25.Cookies in API Handlers
⏱️ 4:25
26.React Suspense Simplified
⏱️ 2:23
27.Power of Suspense Boundaries
⏱️ 3:52
28.use Promises
⏱️ 3:24
29.Stream Promise Results from Server to Client
⏱️ 3:03
30.use Requires a Cached Promise
free
⏱️ 2:30
31.Suspense Error Boundaries
⏱️ 4:42
32.React Server Functions
⏱️ 4:50
33.Server Actions for Powerful Forms
⏱️ 5:35
34.useActionState to Simplify Form Actions
⏱️ 6:38
35.React Serializable Types
⏱️ 2:42
36.Create Dynamic Images with NextJS ImageResponse
⏱️ 5:54
37.Deploy NextJS Application to Production
⏱️ 3:51
38.NextJS Environment Variables Masterclass
⏱️ 3:50
39.Deploying and Managing Environment Variables
⏱️ 4:38
40.Using Databases for Dynamic Server Side State
free
⏱️ 7:34
41.Mastering NextJS Layout Files
⏱️ 4:54
42.Client State Management with NextJS
⏱️ 3:48
43.Client State Management In Action
⏱️ 8:04
44.Using Client State Management Libraries
⏱️ 5:25
45.Detecting Server vs Client Runtime and useEffect
⏱️ 4:33
46.Demystifying ISR vs SSR vs SSG
⏱️ 3:07
47.Static Site Generation or SSG with NextJS
⏱️ 3:16
48.Incremental Static Regeneration (ISR) Simplified
⏱️ 2:51
49.API Routes Caching with NextJS
⏱️ 3:05
50.Reference Guide to Caching for Modern NextJS
⏱️ 2:47
51.Setup a BASE_URL for client and server
free
⏱️ 2:54
52.Creating a sitemap.xml
⏱️ 3:29
53.Provide metadata with NextJS
⏱️ 6:54
54.Created High Quality Social Media Previews
⏱️ 7:38