concept promise in category react

appears as: promise, promises
Redux in Action

This is an excerpt from Manning's book Redux in Action.

Listing 4.21. src/actions/index.js
function fetchTasksFailed(error) { return { type: 'FETCH_TASKS_FAILED', payload: { error, }, }; } export function fetchTasks() { return dispatch => { dispatch(fetchTasksStarted()); api .fetchTasks() .then(resp => { // setTimeout(() => { #1 // dispatch(fetchTasksSucceeded(resp.data)); #1 // }, 2000); #1 throw new Error('Oh noes! Unable to fetch tasks!')); #2 }) .catch(err => { dispatch(fetchTasksFailed(err.message)); #3 }); }; }; ... #1 Comments out the success handler for now #2 Manually rejects the promise, to test the code in the catch block #3 Dispatches another synchronous action with an error message !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":16,\"ch\":63},{\"line\":16,\"ch\":65}],[{\"line\":17,\"ch\":63},{\"line\":17,\"ch\":65}],[{\"line\":18,\"ch\":63},{\"line\":18,\"ch\":65}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":2}],[{\"line\":16,\"ch\":63},{\"line\":16,\"ch\":65}],[{\"line\":17,\"ch\":63},{\"line\":17,\"ch\":65}],[{\"line\":18,\"ch\":63},{\"line\":18,\"ch\":65}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":2}],[{\"line\":16,\"ch\":63},{\"line\":16,\"ch\":65}],[{\"line\":17,\"ch\":63},{\"line\":17,\"ch\":65}],[{\"line\":18,\"ch\":63},{\"line\":18,\"ch\":65}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":2}],[{\"line\":19,\"ch\":63},{\"line\":19,\"ch\":65}],[{\"line\":29,\"ch\":0},{\"line\":29,\"ch\":2}],[{\"line\":22,\"ch\":63},{\"line\":22,\"ch\":65}],[{\"line\":30,\"ch\":0},{\"line\":30,\"ch\":2}]]"} !@%STYLE%@!

To make testing error handling easier, manually reject a promise in the then block, so that you’re guaranteed to make it into the catch block, as shown in the following listing.

Listing 4.21. src/actions/index.js
function fetchTasksFailed(error) { return { type: 'FETCH_TASKS_FAILED', payload: { error, }, }; } export function fetchTasks() { return dispatch => { dispatch(fetchTasksStarted()); api .fetchTasks() .then(resp => { // setTimeout(() => { #1 // dispatch(fetchTasksSucceeded(resp.data)); #1 // }, 2000); #1 throw new Error('Oh noes! Unable to fetch tasks!')); #2 }) .catch(err => { dispatch(fetchTasksFailed(err.message)); #3 }); }; }; ... #1 Comments out the success handler for now #2 Manually rejects the promise, to test the code in the catch block #3 Dispatches another synchronous action with an error message !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":16,\"ch\":63},{\"line\":16,\"ch\":65}],[{\"line\":17,\"ch\":63},{\"line\":17,\"ch\":65}],[{\"line\":18,\"ch\":63},{\"line\":18,\"ch\":65}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":2}],[{\"line\":16,\"ch\":63},{\"line\":16,\"ch\":65}],[{\"line\":17,\"ch\":63},{\"line\":17,\"ch\":65}],[{\"line\":18,\"ch\":63},{\"line\":18,\"ch\":65}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":2}],[{\"line\":16,\"ch\":63},{\"line\":16,\"ch\":65}],[{\"line\":17,\"ch\":63},{\"line\":17,\"ch\":65}],[{\"line\":18,\"ch\":63},{\"line\":18,\"ch\":65}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":2}],[{\"line\":19,\"ch\":63},{\"line\":19,\"ch\":65}],[{\"line\":29,\"ch\":0},{\"line\":29,\"ch\":2}],[{\"line\":22,\"ch\":63},{\"line\":22,\"ch\":65}],[{\"line\":30,\"ch\":0},{\"line\":30,\"ch\":2}]]"} !@%STYLE%@!

Things get more complicated when the contents of that modal need to be populated by an AJAX call. You could choose, for example, to dispatch an action only after the AJAX call returns, or dispatch several actions along the way to indicate loading progress. Again, the store deals in actions and is unequipped to handle functions or promises, so it’s up to you to make sure that whatever reaches the store is an action.

6.9.2. Handling promises with redux-promise

The redux-promise library is another tool maintained by Redux co-creator, Andrew Clark. The premise is simple: whereas redux-thunk allows action creators to return functions, redux-promise allows action creators to return promises.

Also, like redux-thunk, redux-promise provides middleware that can be applied during the creation of the store. Several functional nuances exist for using promises over thunks, or both in tandem, but choosing is largely a matter of style preference. The package is available on GitHub at https://github.com/acdlite/redux-promise.