Last active
December 5, 2019 21:54
-
-
Save christianchown/73a5ebc914e5303e0a00833c82cd3109 to your computer and use it in GitHub Desktop.
Custom async Redux middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const userMiddleware = ({ getState, dispatch }) => next => async action => { | |
const result = next(action); | |
switch (action.type) { | |
case SOME_ACTION: | |
const asyncResult = await somethingAsync(); | |
dispatch(anotherAction(asyncResult)); | |
break; | |
case SOME_OTHER_ACTION: | |
const { slice: { stateVariable } } = getState(); | |
await someProcess(stateVariable); | |
break; | |
} | |
return result; | |
} |
@Constantiner it's really funny to meet you here as the only commentator! :) RIA times ;)
@plandem :)
@Constantiner good spot! I originally didn't have the method flagged as async, and had that to return a Promise... will change, thanks!
Hmm. One thing to note: by marking it as async action =>
, you are now causing it to always return a promise, which also wraps everything returned by the middleware pipeline after this in a promise as well.
This may not be an issue depending on your setup, but FYI.
omg, that gist is like meeting of old friends now. Before it was @Constantiner and now @markerikson (hello, redux-orm) :)))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry for the stupid question, but why you need this:
from
async
function consideringasync
functions always returnPromise
and it will be the same as just:?