Skip to content

Commit 2817d8b

Browse files
provide better examples of async iterators usage
1 parent 9d12596 commit 2817d8b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

docs/README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# React Coroutine
2-
31
Small library which leverages the power of modern JavaScript to provide seamless way in creating stateful components with different purposes.
42

53
> **Coroutines** are computer program components that generalize subroutines for nonpreemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations. Coroutines are well-suited for implementing more familiar program components such as cooperative tasks, exceptions, event loop, iterators, infinite lists and pipes.
@@ -92,21 +90,25 @@ In the example above, the result component receives `userId` property and uses i
9290

9391
[Asynchronous Iterators](https://github.com/tc39/proposal-async-iteration) are in [Stage 3](https://github.com/tc39/proposals) and soon will be added to the language. However, you might want to try them right now, using [Babel](https://babeljs.io/) or [async-to-gen](https://github.com/leebyron/async-to-gen).
9492

93+
You can use async iterators for providing more than one step of rendering. For example, it can be loading spinner that is rendered immediately and once data is fetched, necessary component replaces the spinner.
94+
9595
```javascript
96-
async function* CommentListContainer() {
96+
async function* SearchResultsContainer({ query }) {
9797
yield <LoadingSpinner />;
9898

9999
try {
100-
const comments = await CommentsDAO.retrieve();
101-
return <CommentList comments={comments} />;
100+
const results = await SearchDAO.retrieve(query);
101+
return <SearchResults results={results} />;
102102
} catch (error) {
103103
return <ErrorMessage error={error} />;
104104
}
105105
}
106106

107-
export default Coroutine.create(CommentListContainer);
107+
export default Coroutine.create(SearchResultsContainer);
108108
```
109109

110+
Also, you might have a component that needs a data from different sources but can be rendered progressively.
111+
110112
```javascript
111113
async function* List() {
112114
try {

0 commit comments

Comments
 (0)