Skip to content

Function: createThunk()

createThunk<T, ThunkBody, Args>(type, thunkBodyCreator, triggers?): ThunkCreator<T, ThunkBody, Args>

Defined in: actions.ts:97

Creates an asynchronous thunk action creator function.

A thunk is a function that can perform asynchronous logic and dispatch multiple actions before and/or after its asynchronous operations complete.

This version also supports "triggers" — action types or matcher functions that, when matched by any dispatched action, will cause this thunk to be executed automatically.

Type Parameters

T

T extends string = string

The string literal type of the thunk's action type.

ThunkBody

ThunkBody extends AsyncAction<any, Record<string, any>> = AsyncAction<any, Record<string, any>>

The type of the thunk function (AsyncAction).

Args

Args extends any[] = any[]

The argument tuple type accepted by the thunk creator.

Parameters

type

T

The action type string for the thunk (used for matching and debugging).

thunkBodyCreator

(...args) => ThunkBody

A factory function that receives the thunk's arguments and returns the actual thunk body function to execute.

triggers?

(string | (action) => boolean)[]

Optional list of trigger definitions. Each trigger can be:

  • a string action type to match exactly, or
  • a matcher function that receives the dispatched action and returns true if the thunk should run.

Returns

ThunkCreator<T, ThunkBody, Args>

A thunk creator function. Calling this function with arguments will return a thunk function with attached metadata:

  • type: the action type string
  • match(action): checks if the given action matches this thunk's type
  • isThunk: true for identification in middleware
  • triggers: (optional) the list of trigger definitions

Released under the MIT License.