-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwip-readme
63 lines (49 loc) · 1.31 KB
/
wip-readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# GENERATOR COMPONENTS (actual name TBD)
Let's say you have a component that needs to load some data and you want to
show a placeholder until the request for that data finishes.
Sure, let's whip that up:
```jsx
class MyComponent extends React.Component {
constructor(...props) {
super(...props);
this.state = {
data: null
};
}
async componentDidMount() {
const r = await fetch('/my/api/route')
const json = await r.json();
this.setState({data: json});
}
render() {
const { data } = this.state;
if (!data) {
return <div>Loading...</div>;
}
return (
<div>
Here's my data:
{JSON.stringify(data)}
</div>
);
}
}
```
Not too exciting, I know, but that's the general pattern these days.
Well, with generator components, you could write this instead:
```jsx
const MyComponent = fromGenerator(
async function* () {
yield <div>Loading...</div>;
const r = await fetch('/my/api/route');
const json = await r.json();
yield (
<div>
Here's my data:
{JSON.stringify(data)}
</div>
);
}
);
```
Async generator functions?!? Oh yes, they're [stage-3](https://github.com/tc39/proposal-async-iteration#implementation-status), so you should see them land in various javascript engines soon.