question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation not clear on getting promise from actions

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
appelgriebschcommented, Apr 7, 2017

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.

1reaction
BryanGrezeszakcommented, Mar 30, 2017

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 just myAction('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:

// defining the action itself
const ProjectActions = Reflux.createActions({
    'createProject': { asyncResult: true }
})

// now defining what actually happens in that action (Reflux alone)
ProjectActions.createProject.listen( function(myArg) {
    somethingPromisy(myArg)
        .then(ProjectActions.createProject.completed);
        .catch(ProjectActions.createProject.failed);
});

// OR, if you use reflux-promise, it can reduce that to a simpler 1-liner
ProjectActions.createProject.listenAndPromise( somethingPromisy );
Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise - JavaScript - MDN Web Docs
A promise is said to be settled if it is either fulfilled or rejected, but not pending. Flowchart showing how the Promise state...
Read more >
vue.js - Returning Promises from Vuex actions - Stack Overflow
Now, when your Vue component initiates myAction , it will get this Promise object and can know whether it succeeded or not.
Read more >
Writing Promise-Using Specifications - W3C
This document gives guidance on how to write specifications that create, ... Thus becoming loaded is not a one-time occasion: instead, ...
Read more >
JavaScript Promises: an introduction - web.dev
Maybe you're not even sure what a "promise" is. ... We get the image, add a couple of listeners, then JavaScript can stop...
Read more >
Docs • Svelte
This page contains detailed API reference documentation. It's intended to be a resource for people who already have some familiarity with Svelte. If...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found