Slide 18
Slide 18 text
// A thunk represents an action that can perform side effects, access the current state of
the store, and dispatch new actions, as if it were a ReSwift middleware.
let thunk = Thunk { dispatch, getState in
if getState!.loading {
return
}
dispatch(RequestStart())
api.getSomething() { something in
if something != nil {
dispatch(RequestSuccess(something))
} else {
dispatch(RequestError())
}
}
}
// As the thunk type conforms to the `Action` protocol, you can dispatch it as usual,
without having to implement an overload of the `dispatch` function inside the ReSwift
library.
store.dispatch(thunk)