mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
When the payload is a Promise, the Promise Middleware will change what `dispatch()` returns. Reflect this with typing. According to documentation [1], it is a Promise of an object with the promised `value` and the FULFILLED action `type`. [1] https://github.com/pburtchaell/redux-promise-middleware/blob/master/docs/guides/chaining-actions.md
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { createStore, applyMiddleware, Store, Dispatch } from "redux";
|
|
import promiseMiddleware from "redux-promise-middleware";
|
|
|
|
declare var rootReducer: (state: any, action: any) => any;
|
|
declare var doSomethingAsyncAndReturnPromise: any;
|
|
declare var someActionCreator: any;
|
|
|
|
const createStoreWithMiddleware = applyMiddleware(
|
|
promiseMiddleware()
|
|
)(createStore);
|
|
|
|
const store: Store<any> = createStoreWithMiddleware(rootReducer);
|
|
|
|
export function myAsyncActionCreator(data: any) {
|
|
return {
|
|
type: "ACTION",
|
|
payload: {
|
|
promise: doSomethingAsyncAndReturnPromise(data),
|
|
data: data
|
|
}
|
|
};
|
|
}
|
|
|
|
const actionCreator1 = () => ({
|
|
type: "FIRST_ACTION_TYPE",
|
|
payload: {
|
|
promise: Promise.resolve({
|
|
type: "SECOND_ACTION_TYPE",
|
|
payload: "...",
|
|
})
|
|
}
|
|
});
|
|
|
|
const actionCreator2 = () => ({
|
|
type: "FIRST_ACTION_TYPE",
|
|
payload: {
|
|
promise: Promise.resolve((action: string, dispatch: Dispatch<any>, getState: Function) => {
|
|
dispatch({ type: "SECEOND_ACTION_TYPE", payload: "..." });
|
|
dispatch(someActionCreator());
|
|
dispatch({ type: "CHAINED_ACTION", payload: Promise.resolve("...") })
|
|
.then(({ type, value }) => {})
|
|
})
|
|
}
|
|
});
|