{"id":16374811,"url":"https://github.com/jameslnewell/redux-when","last_synced_at":"2025-09-23T17:59:43.739Z","repository":{"id":44210402,"uuid":"53094907","full_name":"jameslnewell/redux-when","owner":"jameslnewell","description":"Redux middleware for delaying dispatch of an action until a condition evaluates to true.","archived":false,"fork":false,"pushed_at":"2023-01-05T08:07:01.000Z","size":493,"stargazers_count":18,"open_issues_count":9,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-12T03:18:25.528Z","etag":null,"topics":["delay","delaying-dispatch","dispatch","redux","redux-middleware","thunk","until"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jameslnewell.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-04T00:35:26.000Z","updated_at":"2020-10-24T19:12:03.000Z","dependencies_parsed_at":"2023-02-03T19:46:36.153Z","dependency_job_id":null,"html_url":"https://github.com/jameslnewell/redux-when","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameslnewell%2Fredux-when","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameslnewell%2Fredux-when/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameslnewell%2Fredux-when/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameslnewell%2Fredux-when/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jameslnewell","download_url":"https://codeload.github.com/jameslnewell/redux-when/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244721284,"owners_count":20498923,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["delay","delaying-dispatch","dispatch","redux","redux-middleware","thunk","until"],"created_at":"2024-10-11T03:18:28.144Z","updated_at":"2025-09-23T17:59:38.718Z","avatar_url":"https://github.com/jameslnewell.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# redux-when\n\nRedux middleware for delaying dispatch of an action until a condition evaluates to true.\n\n##### Why\n\nUsually, you use promises to chain asynchronous actions:\n\n```js\n/*\n Save the form and navigate somewhere\n */\nconst handleFormSubmit = () =\u003e {\n  Promise.resolve()\n    .then(() =\u003e store.dispatch(save(data)))\n    .then(() =\u003e store.dispatch(navigate()))\n  ;\n}\n```\n\nBut sometimes asynchronous actions have already been dispatched and you don't have a promise to chain on. `redux-when` was created to solve this problem:\n\n```js\n\n/*\n Save the form\n */\nconst handleFieldBlur = () =\u003e {\n  store.dispatch(save(data))\n};\n\n/*\n Wait for any save actions to finish and then navigate somewhere\n */\nconst handleFormSubmit = () =\u003e {\n  store.dispatch(once(state =\u003e state.saved, navigate()));\n}\n\n```\n\n## Installation\n\n    npm install --save redux-when\n\n## Usage\n\n```javascript\nimport {createStore, applyMiddleware} from 'redux';\nimport middleware, {once} from 'redux-when';\n\nconst reducer = (state = {}, action = {}) =\u003e {\n  switch (action.type) {\n\n    case 'SAVE':\n      return {...state, saved: true};\n\n    case 'NAVIGATE':\n      return {...state, navigated: true};\n\n    default:\n      return state;\n\n  }\n};\n\n//create the store\nconst store = createStore(reducer, {}, applyMiddleware(middleware));\n\n//dispatch the `NAVIGATE` action ONCE the state has been saved\nstore.dispatch(once(state =\u003e state.saved, () =\u003e ({type: 'NAVIGATE'})));\n\n//prints: {}\nconsole.log(store.getState());\n\n//dispatch the `SAVE` action which will update the state and trigger\n// the delayed `NAVIGATE` action\nstore.dispatch({type: 'SAVE'});\n\n//prints: {saved: true, navigated: true}\nconsole.log(store.getState());\n\n```\n\n## API\n\n### middleware\n\nThe Redux middleware.\n\n### once(condition, createAction)\n\nCreates an action that will execute ONCE when the `condition` evaluates to true.\n\n**Parameters:**\n\n- `condition : (state : object, action : object) =\u003e boolean` \u0026mdash; Required. The condition that determines when the action is dispatched.\n\n- `createAction : (action : object) =\u003e *` \u0026mdash; Required. A function creating the action that will be dispatched when the `condition` evaluates to `true`. Can return any value dispatchable by your store including thunks, promises etc as long as your store is configured with the necessary middleware.\n\n**Returns:**\n\nReturns a dispatchable Redux action that will be handled by the `redux-when` middleware. When dispatched, a `cancel` token will be returned.\n\n\u003e Note: You MUST dispatch the action created by `once()` otherwise nothing will happen.\n  ```js\n  store.dispatch(once(() =\u003e true, () =\u003e {type: 'XYZ'}));\n  ```\n\n### when(condition, createAction)\n\nCreates an action that will execute EVERY time the `condition` evaluates to true.\n\n**Parameters:**\n\n- `condition : (state : object, action : object) =\u003e boolean` \u0026mdash; Required. The condition that determines when the action is dispatched.\n\n- `createAction : (action : object) =\u003e *` \u0026mdash; Required. A function creating the action that will be dispatched when the `condition` evaluates to `true`. Can return any value dispatchable by your store including thunks, promises etc as long as your store is configured with the necessary middleware.\n\n**Returns:**\n\nReturns a dispatchable Redux action that will be handled by the `redux-when` middleware. When dispatched, a `cancel` token will be returned.\n\n\u003e Note: You MUST dispatch the action created by `when()` otherwise nothing will happen.\n  ```js\n  store.dispatch(when(() =\u003e true, () =\u003e {type: 'XYZ'}));\n  ```\n\n### cancel(token)\n\nCancels a delayed action created by `once()` or `when()`.\n\n**Parameters:**\n\n- `token : *` \u0026mdash; Required. A token returned by `once()` or `when()`.\n\n**Returns:**\n\nReturns a dispatchable Redux action that will be handled by the `redux-when` middleware. When dispatched, `null` will be returned.\n\n\u003e Note: You MUST dispatch the action created by `cancel()` otherwise nothing will happen.\n\n```js\nimport React from 'react';\nimport {connect} from 'react-redux';\n\nclass Example extends React.Component {\n\n  componentWillMount() {\n    this.token = this.props.dispatch(when(() =\u003e true, () =\u003e {type: 'XYZ'));\n  }\n\n  componentWillUnmount() {\n    this.token = this.props.dispatch(cancel(this.token));\n  }\n\n}\n\nexport default connect()(Example);\n```\n\n# Change log\n\n[Change log](https://github.com/jameslnewell/redux-when/blob/master/README.md).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjameslnewell%2Fredux-when","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjameslnewell%2Fredux-when","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjameslnewell%2Fredux-when/lists"}