-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathindex.js
51 lines (45 loc) · 1.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { isFSA } from 'flux-standard-action';
import uniqueId from 'lodash/utility/uniqueId';
function isPromise(val) {
return val && typeof val.then === 'function';
}
export default function promiseMiddleware({ dispatch }) {
return next => action => {
if (!isFSA(action)) {
return isPromise(action)
? action.then(dispatch)
: next(action);
}
if (isPromise(action.payload)) {
const sequenceId = uniqueId();
dispatch({
...action,
payload: undefined,
sequence: {
type: 'start',
id: sequenceId
}
});
return action.payload.then(
result => dispatch({
...action,
payload: result,
sequence: {
type: 'next',
id: sequenceId
}
}),
error => dispatch({
...action,
payload: error,
error: true,
sequence: {
type: 'next',
id: sequenceId
}
})
);
}
return next(action);
};
}