Skip to content

Commit bff1f53

Browse files
committed
Merge pull request #606 from mareksuscak/fix-failing-promises-example
Fix failing Promises example from the documentation.
2 parents 1a04e9f + cd4da3a commit bff1f53

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/actions/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@ export default function makeAction(alt, namespace, name, implementation, obj) {
1212

1313
// the action itself
1414
const action = (...args) => {
15-
const result = implementation.apply(obj, args)
15+
const invocationResult = implementation.apply(obj, args)
16+
let actionResult = invocationResult
1617

1718
// async functions that return promises should not be dispatched
18-
if (result !== undefined && !isPromise(result)) {
19-
if (fn.isFunction(result)) {
20-
result(dispatch, alt)
19+
if (invocationResult !== undefined && !isPromise(invocationResult)) {
20+
if (fn.isFunction(invocationResult)) {
21+
// inner function result should be returned as an action result
22+
actionResult = invocationResult(dispatch, alt)
2123
} else {
22-
dispatch(result)
24+
dispatch(invocationResult)
2325
}
2426
}
2527

26-
if (result === undefined) {
28+
if (invocationResult === undefined) {
2729
utils.warn('An action was called but nothing was dispatched')
2830
}
2931

30-
return result
32+
return actionResult
3133
}
3234
action.defer = (...args) => setTimeout(() => action.apply(null, args))
3335
action.id = id

test/async-action-test.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Alt from '../'
22
import { assert } from 'chai'
3+
import isPromise from 'is-promise'
34

45
const alt = new Alt()
56

@@ -8,6 +9,13 @@ const actions = alt.createActions(class AsyncActions {
89
fetch() {
910
return Promise.resolve('foo')
1011
}
12+
13+
fetchAndDispatch() {
14+
return (dispatch) => {
15+
dispatch()
16+
return Promise.resolve('foo')
17+
}
18+
}
1119
})
1220

1321
const store = alt.createStore(class FooStore {
@@ -19,11 +27,26 @@ const store = alt.createStore(class FooStore {
1927
onFetch() {
2028
this.dispatched = true
2129
}
30+
onFetchAndDispatch() {
31+
this.dispatched = true
32+
}
2233
})
2334

2435
export default {
25-
'async actions'() {
26-
actions.fetch()
27-
assert(store.state.dispatched === false, 'async action is not automatically dispatched')
28-
}
36+
'async actions': {
37+
afterEach() {
38+
alt.recycle(store)
39+
},
40+
41+
'are not dispatched automatically'() {
42+
actions.fetch()
43+
assert(store.state.dispatched === false, 'async action is not automatically dispatched')
44+
},
45+
46+
'return the result of inner function invocation'() {
47+
const promise = actions.fetchAndDispatch()
48+
assert(isPromise(promise), 'async action does not return the result of inner function invocation')
49+
assert(store.state.dispatched === true, 'async action is dispatched when the dispatch is invoked manually')
50+
},
51+
},
2952
}

0 commit comments

Comments
 (0)