Created
July 12, 2018 20:58
-
-
Save evaldosantos/a6d936ff1222e79c57d620738d1a9649 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export default function createAsyncAction (type, fn) { | |
return (...args) => async (dispatch) => { | |
dispatch({ | |
type: `${type}_STARTED`, | |
payload: args | |
}); | |
let result; | |
try { | |
result = await fn(...args); | |
} catch (error) { | |
dispatch({ | |
type: `${type}_FAILED`, | |
error: true, | |
payload: error | |
}); | |
throw error; | |
} | |
dispatch({ | |
type: `${type}_ENDED`, | |
payload: result | |
}); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment