{"action":"created","issue":{"url":"https://api.github.com/repos/jeffbski/redux-logic/issues/93","repository_url":"https://api.github.com/repos/jeffbski/redux-logic","labels_url":"https://api.github.com/repos/jeffbski/redux-logic/issues/93/labels{/name}","comments_url":"https://api.github.com/repos/jeffbski/redux-logic/issues/93/comments","events_url":"https://api.github.com/repos/jeffbski/redux-logic/issues/93/events","html_url":"https://github.com/jeffbski/redux-logic/issues/93","id":302408651,"number":93,"title":"Make `dispatch` promisified","user":{"login":"artificis","id":14348482,"avatar_url":"https://avatars1.githubusercontent.com/u/14348482?v=4","gravatar_id":"","url":"https://api.github.com/users/artificis","html_url":"https://github.com/artificis","followers_url":"https://api.github.com/users/artificis/followers","following_url":"https://api.github.com/users/artificis/following{/other_user}","gists_url":"https://api.github.com/users/artificis/gists{/gist_id}","starred_url":"https://api.github.com/users/artificis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/artificis/subscriptions","organizations_url":"https://api.github.com/users/artificis/orgs","repos_url":"https://api.github.com/users/artificis/repos","events_url":"https://api.github.com/users/artificis/events{/privacy}","received_events_url":"https://api.github.com/users/artificis/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-03-05T18:22:37Z","updated_at":"2018-03-05T22:07:41Z","closed_at":null,"author_association":"NONE","body":"For most of my projects, I have to make multiple `dispatch` calls in `process` and I normally write like this:\r\n\r\n```js\r\nconst logic = createLogic({\r\n  type: FOO,\r\n  async process({ getState, action }, dispatch, done) {\r\n    try {\r\n      const json = await api.get(...).getBody();\r\n      dispatch(setAuthToken(json.token));\r\n      dispatch(fetchUser());\r\n    } finally {\r\n      done();\r\n    }\r\n  }\r\n});\r\n```\r\n\r\nBut this (very occasionally) fails, because `fetchUser` relies on a valid auth token and sometimes it was called before `setAuthToken` was completed, despite being dispatched order.\r\n\r\nWhat about making dispatch a function that returns a promise? So that I can use `await` in front of it. Like this:\r\n\r\n```js\r\nawait dispatch(setAuthToken(json.token));\r\ndispatch(fetchUser());\r\n```\r\n\r\nI can do monkeypatching for redux's `store.dispatch` function, but it looks like redux-logic's dispatch is different."},"comment":{"url":"https://api.github.com/repos/jeffbski/redux-logic/issues/comments/370584487","html_url":"https://github.com/jeffbski/redux-logic/issues/93#issuecomment-370584487","issue_url":"https://api.github.com/repos/jeffbski/redux-logic/issues/93","id":370584487,"user":{"login":"artificis","id":14348482,"avatar_url":"https://avatars1.githubusercontent.com/u/14348482?v=4","gravatar_id":"","url":"https://api.github.com/users/artificis","html_url":"https://github.com/artificis","followers_url":"https://api.github.com/users/artificis/followers","following_url":"https://api.github.com/users/artificis/following{/other_user}","gists_url":"https://api.github.com/users/artificis/gists{/gist_id}","starred_url":"https://api.github.com/users/artificis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/artificis/subscriptions","organizations_url":"https://api.github.com/users/artificis/orgs","repos_url":"https://api.github.com/users/artificis/repos","events_url":"https://api.github.com/users/artificis/events{/privacy}","received_events_url":"https://api.github.com/users/artificis/received_events","type":"User","site_admin":false},"created_at":"2018-03-05T22:07:41Z","updated_at":"2018-03-05T22:07:41Z","author_association":"NONE","body":"I replaced this monkeypatching with a redux middleware since monkeypatching looks a little hacky. Here's my simple middleware:\r\n\r\n```js\r\nfunction promisifyDispatchMiddleware() {\r\n  return next => action =>\r\n    new Promise(resolve => {\r\n      if (action.type) {\r\n        EE.once(`redux-action/${action.type}`, resolve);\r\n      }\r\n      next(action);\r\n    });\r\n}\r\n```"}}
{"actor":{"display_login":"artificis"}}