File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -27,20 +27,27 @@ function fetchData() {
2727 return fetch (exampleURL);
2828}
2929
30+ // Define a machine just using functions
3031function Loader () {
32+ // Each state is a generator function
3133 function * idle () {
3234 yield on (" FETCH" , loading);
3335 }
36+ // This is the ‘loading’ state
3437 function * loading () {
3538 yield entry (fetchData);
3639 yield on (" SUCCESS" , success);
3740 yield on (" FAILURE" , failure);
3841 }
42+ // States can be final
3943 function * success () {}
44+ // Or they can define transitions to other states
4045 function * failure () {
46+ // When the RETRY event happens, we transition from ‘failure’ to ‘loading’
4147 yield on (" RETRY" , loading);
4248 }
4349
50+ // Return the initial state from your machine definition
4451 return idle;
4552}
4653
@@ -104,10 +111,6 @@ loader.resolved.then(([response]) => {
104111 // Use response of fetch()
105112 loader .value ; // "success"
106113});
107-
108- /* Or with await: */
109- // const [response] = await loader.resolved;
110- // loader.value; // "success"
111114```
112115
113116----
You can’t perform that action at this time.
0 commit comments