forked from osdio/noder-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromiseMiddleware.js
More file actions
61 lines (55 loc) · 987 Bytes
/
promiseMiddleware.js
File metadata and controls
61 lines (55 loc) · 987 Bytes
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
52
53
54
55
56
57
58
59
60
61
import { isFSA } from 'flux-standard-action';
import _ from 'lodash';
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);
}
const { meta = {}, payload } = action;
const id = _.uniqueId();
if (isPromise(payload)) {
dispatch({
...action,
payload: undefined,
meta: {
...meta,
sequence: {
type: 'start',
id
}
}
});
return payload.then(
result => dispatch({
...action,
payload: result,
meta: {
...meta,
sequence: {
type: 'next',
id
}
}
}),
error => dispatch({
...action,
payload: error,
error: true,
meta: {
...meta,
sequence: {
type: 'next',
id
}
}
})
);
}
return next(action);
};
}