Skip to content

Commit 3da0e4c

Browse files
committed
Make the combined example's data delay consistent between runs
1 parent 0ba3d42 commit 3da0e4c

1 file changed

Lines changed: 8 additions & 16 deletions

File tree

‎src/content/reference/react/Suspense.md‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3036,7 +3036,7 @@ import { freshStylesheetUrl, freshImageUrl } from './resources.js';
30363036
import VanillaProfileCard from './VanillaProfileCard.js';
30373037

30383038
function ProfileCard({ resources }) {
3039-
const quote = use(fetchQuote());
3039+
const quote = use(resources.quotePromise);
30403040
return (
30413041
<>
30423042
<link rel="stylesheet" href={resources.stylesheet} precedence="default" />
@@ -3071,6 +3071,7 @@ export default function App() {
30713071
onClick={() => {
30723072
startTransition(() => {
30733073
setResources({
3074+
quotePromise: fetchQuote(),
30743075
stylesheet: freshStylesheetUrl(),
30753076
image: freshImageUrl(),
30763077
});
@@ -3150,22 +3151,13 @@ export function freshImageUrl() {
31503151
```js src/data.js hidden
31513152
// Note: the way you would do data fetching depends on
31523153
// the framework that you use together with Suspense.
3153-
// Normally, the caching logic would be inside a framework.
31543154

3155-
let cache = null;
3156-
3157-
export function fetchQuote() {
3158-
if (!cache) {
3159-
cache = new Promise((resolve) => {
3160-
// Add a fake delay to make waiting noticeable.
3161-
setTimeout(() => {
3162-
resolve(
3163-
'The best way to predict the future is to invent it.'
3164-
);
3165-
}, 2000);
3166-
});
3167-
}
3168-
return cache;
3155+
export async function fetchQuote() {
3156+
// Add a fake delay to make waiting noticeable.
3157+
await new Promise((resolve) => {
3158+
setTimeout(resolve, 2000);
3159+
});
3160+
return 'The best way to predict the future is to invent it.';
31693161
}
31703162
```
31713163

0 commit comments

Comments
 (0)