Swiftpack.co - Package - ReSwift/ReSwift-Thunk


Build Status Code coverage status CocoaPods Compatible Platform support License MIT Reviewed by Hound

Supported Swift Versions: Swift 4.2, Swift 5.0, Swift 5.3

When ReSwift is a Redux-like implementation of the unidirectional data flow architecture in Swift, ReSwift-Thunk is like redux-thunk.

Why Use ReSwift-Thunk?


// First, you create the middleware, which needs to know the type of your `State`.
let thunkMiddleware: Middleware<MyState> = createThunkMiddleware()

// Note that it can perfectly live with other middleware in the chain.
let store = Store<MyState>(reducer: reducer, state: nil, middleware: [thunkMiddleware])

// 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<MyState> { dispatch, getState in 
    if getState!.loading {
    api.getSomething() { something in
        if something != nil {
        } else {

// A thunk can also be a function if you want to pass on parameters
func thunkWithParams(_ identifier: Int) -> Thunk<MyState> {
    return Thunk<MyState> { dispatch, getState in
        guard let state = getState() else { return }
        if state.loading {
        api.getSomethingWithId(identifier) { something in
            if something != nil {
            } else {

// 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.

// You can do the same with the Thunk that requires parameters, like so

// Note that these actions won't reach the reducers, instead, the thunks middleware will catch it and execute its body, producing the desired side effects.


The ExpectThunk helper, available as a CocoaPods subspec, allows for testing the order and actions of dispatch as well as the dependencies on getState.

    .getsState(RequestState(loading: false))
    // If the action is Equatable it will be asserted for equality with `dispatches`.
    .dispatches { action in
        XCTAssert(action.something == expectedSomething)
    .wait() // or simply run() for synchronous flows


ReSwift-Thunk requires the ReSwift base module.


You can install ReSwift-Thunk via CocoaPods by adding it to your Podfile:

target 'TARGET' do
    pod 'ReSwiftThunk'

target 'TARGET-TESTS' do
    pod 'ReSwiftThunk/ExpectThunk'

And run pod install.

A Note on Including ExpectThunk

If the ExpectThunk subspec is used, the tests target cannot be nested in another target due to current limitations. The tests target must be a standalone target as shown in the snippet above.


You can install ReSwift-Thunk via Carthage by adding the following line to your Cartfile:

github "ReSwift/ReSwift-Thunk"

Swift Package Manager

You can install ReSwift-Thunk via Swift Package Manager by adding the following line to your Package.swift:

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/ReSwift/ReSwift-Thunk.git", majorVersion: XYZ)

Checking out Source Code

After checking out the project run pod install to get the latest supported version of SwiftLint, which we use to ensure a consistent style in the codebase.

Example Projects

  • ReduxMovieDB: A simple App that queries the tmdb.org API to display the latest movies. Allows searching and viewing details. Relevant file.


You can find all the details on how to get started in the Contributing Guide.



ReSwift-Thunk Copyright (c) 2018 ReSwift Contributors. Distributed under the MIT License (MIT). See LICENSE.md.


Stars: 70


Used By

Total: 0


Fix Carthage Build Errors - 2020-10-19 16:01:52


  • Fix Carthage build error (#44) - @DivineDominion, @mjarvis

ReSwift 6.0 - 2020-10-16 20:22:01

Breaking API Changes:

  • Drop support for Swift 3.2, 4.0, and 4.1. (#45) - @shawnkoh, @mjarvis
  • Drop support for iOS 8 (#45) - @shawnkoh, @mjarvis


  • Rename SwiftPM library to ReSwiftThunk, this makes naming consistent across all package manager (Cocoapods, Carthage and SwiftPM) (#42) - @jookes
  • ExpectThunk's methods dispatches and getsState no longer have @discardableResult return values (#40) - @xavierLowmiller

1.2.0 – ReSwift 5 and ExpectThunk - 2019-09-07 14:58:50

API Changes:

  • Renames createThunksMiddleware to createThunkMiddleware and adds deprecated forward for createThunksMiddleware (#20) - @fbernutz


  • Adds ExpectThunk testing helper and corresponding CocoaPods subspec (#19, #37) - @jjgp, @okaverin
  • Adds SwiftPM support (#21) - @jayhickey
  • Require ReSwift 5.0 (#28) - @DivineDominion
  • Specify all officially supported Swift versions in podspec (#38) - @okaverin

1.1.0 - 2019-01-17 17:03:52

API Changes:

  • Renames ThunkAction to Thunk
  • Renames ThunkMiddleware() to createThunkMiddleware()
  • Adds deprecated forwards for ThunkAction and ThunkMiddleware()


  • This project has been migrated from https://github.com/mikecole20/ReSwiftThunk/ along with some backwards-compatible API changes documented above.

Pre-release version for tests with CocoaPods - 2018-11-24 09:04:40