Skip to content

Commit 2df3054

Browse files
committed
Refactor to use async/await
1 parent cfa14e2 commit 2df3054

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ resolved.
8181

8282
See the [TodosContainer](common/js/containers/Todos/index.js) for an example.
8383

84+
## Async / Await
85+
This project uses `async/await`, available by default in Node.js v8.x.x or
86+
higher. If you experience errors, please upgrade your version of Node.js.
87+
8488
## Writing Tests
8589
The default testing framework is Mocha, though you can use whatever you want.
8690
Make sure you have it installed:

common/js/actions/todos.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ export const fetchTodosSuccess = generateActionCreator(FETCH_TODOS_SUCCESS, 'tod
1414
export const fetchTodosFailure = generateActionCreator(FETCH_TODOS_FAILURE, 'error');
1515

1616
export const fetchTodos = () => {
17-
return (dispatch) => {
17+
return async (dispatch) => {
1818
dispatch(fetchTodosRequest());
1919

20-
return fetch('/api/todos', { method: 'GET' })
21-
.then(res => res.json())
22-
.then(todos => dispatch(fetchTodosSuccess(todos)))
23-
.catch(error => dispatch(fetchTodosFailure(error)));
20+
try {
21+
const response = await fetch('/api/todos', { method: 'GET' });
22+
const todos = await response.json();
23+
dispatch(fetchTodosSuccess(todos));
24+
} catch (e) {
25+
dispatch(fetchTodosFailure(e.message));
26+
}
2427
};
2528
};

0 commit comments

Comments
 (0)