Skip to content

Add an optional 'callback' to dispatch #1096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,17 @@ export default function createStore(reducer, initialState) {
* a `type` property which may not be `undefined`. It is a good idea to use
* string constants for action types.
*
* @param {Function} callback An optional function to be called when any and all
* promises returned by listeners have been resolved. This can be used for cases
* when you need to perform a callback after asyncronous changes or rendering
* caused by an action have been completed.
*
* @returns {Object} For convenience, the same action object you dispatched.
*
* Note that, if you use a custom middleware, it may wrap `dispatch()` to
* return something else (for example, a Promise you can await).
*/
function dispatch(action) {
function dispatch(action, callback=null) {
if (!isPlainObject(action)) {
throw new Error(
'Actions must be plain objects. ' +
Expand All @@ -123,7 +128,16 @@ export default function createStore(reducer, initialState) {
isDispatching = false
}

listeners.slice().forEach(listener => listener())
var listenerPromises = listeners.slice().reduce((promises, listener) => {
var response = listener()
if (response && typeof response.then === 'function') {
promises.push(response)
}
return promises
}, [])

Promise.all(listenerPromises).then(callback)

return action
}

Expand Down