Documentation not clear on getting promise from actions
See original GitHub issueI know theres been a few issues in the past around async actions and promises, namely #216 and #223. However, I feel as though the documentation for Reflux still lacks in this area, and as a user on the newer versions of reflux (3+), I cant work out how I’m supposed to get promises from actions. I have tried using the reflux-promise library with refluxjs 6+, however when I try:
const ProjectActions = Reflux.createActions([{
'createProject': { asyncResult: true }
}])
ProjectActions.createProject().then(...).catch(...)
I receive an error telling me .then() is undefined
For example say I have a new object form (component) that calls an action - in turn a store - that POSTS to a web API when the user submits. I need to know the result of that action in order to know how I should respond i.e. should I take them to the page that shows them the new object in detail, or should I stay and show an error. I don’t think it makes sense to put this routing logic inside any of the stores, because this routing is specific to this component - i.e. I could create an object (or many) from another component and not want to route.
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (4 by maintainers)
Top GitHub Comments
I’ve just had the same problem and found a solution more by incident (aka trail n error).
If you work with a module system (import, require, …) usually the modules get initialised with their own private version of the Reflux object (aka they don’t use the global window object). As of this you might have to duplicate the Reflux.use(RefluxPromise(Promise)) call to all your action modules (see https://github.com/appelgriebsch/electron-shell-services/blob/master/actions/TranslationManager.jsx#L3-L5). Additionally any action call has to be triggered with triggerAsync to get the promise object in return….(see https://github.com/appelgriebsch/electron-shell/blob/master/app/shell/Shell.jsx#L80-L107).
That works for me.
As far as Reflux itself lacking documentation on it, that’s by design. It is all split out into reflux-promise, and the promise based docs are all in there, not in Reflux itself.
As for the actions not returning the promise, I think that’s a bug introduced when a few action changes were made a bit ago. I’ll look into it. But in the mean time you can likely just get around it by calling
myAction.triggerAsync('foo')
directly instead of justmyAction('foo')
However, I’m also confused by some stuff you’re saying. You’re talking about defining how an action would work…but you’re giving examples in the context of calling that action…not of defining behaviors of that action.
Defining what an async action does goes something like this: