GithubHelp home page GithubHelp logo

Comments (14)

tomatau avatar tomatau commented on May 18, 2024 1

Maybe this? (with latest version)

export function createAndStart(name) {
  return (dispatch) => {
    dispatch({
      type: CREATE_AND_START,
      payload: {
        promise: new Promise(async (resolve, reject) => {
          await dispatch(create(name)).payload.promise
          await dispatch(start()).payload.promise
          resolve(push('/start'))
        })
      }
    });
  };
}

Have a look at our tests to get more of an idea of the return from dispatch calls when using a promise-middleware-action

from redux-promise-middleware.

pburtchaell avatar pburtchaell commented on May 18, 2024

I'm busy for the rest of the week. I'll get back to you this weekend when I have some more time to read through your question to give you a worthwhile answer!

from redux-promise-middleware.

baronnoraz avatar baronnoraz commented on May 18, 2024

Thanks! Sorry to be a bother, I really want to learn the/a proper way to do things. I'm grateful for the willingness to help.

from redux-promise-middleware.

rkoberg avatar rkoberg commented on May 18, 2024

I was looking for a way to do this type of thing as well. This works, but was wondering if there was a better way:

return ({fetch, settings: {firebaseUrl}}) => {
    return ({
      type: 'LOAD_PROFILE_USER',
      payload: {
        promise: fetch(`${firebaseUrl}/users/${slug}.json`)
          .then(response => response.json())
          .then(response => (users.viewer && users.viewer.slug === response.slug) ?
            Promise.resolve(loadProfileSettingsForm(response)) :
            Promise.resolve(true))
          .catch(err => {
            throw err;
          }),
      }
    });
  };

from redux-promise-middleware.

rkoberg avatar rkoberg commented on May 18, 2024

Also, if there is some way to do this with aysnc/await?

from redux-promise-middleware.

baronnoraz avatar baronnoraz commented on May 18, 2024

@tomatau Thank you!! That worked perfectly. I truly appreciate the help, and I'll be sure to dig into the tests to learn more.

@rkoberg Hopefully this example using async/await helps you too...

from redux-promise-middleware.

rkoberg avatar rkoberg commented on May 18, 2024

Sure, but I just moved it to a separate async function (example below). Using Este, it takes care of dispatch for actions:
https://github.com/este/este/blob/master/src/common/configureStore.js#L23
https://github.com/este/este/blob/master/src/common/lib/injectDependencies.js

However, it just uses the dispatch function (not always a promise). What is the benefit of appending .payload.promise? I want access to the data to transform for the reducer. And I am not clear what you are doing with resolve(push('/start')). What is that for?

Here is what I am doing:

async function _login(firebase, fields) {
  try {
    const authData = await firebase.authWithPassword(fields.toJS());
    const userData = await getUserFromAuthData(firebase, authData);
    return {authData, userData};
  } catch (err) { //eslint-disable-line brace-style
    throw err;
  }
}

from redux-promise-middleware.

baronnoraz avatar baronnoraz commented on May 18, 2024

@rkoberg the resolve(push('/start')) bit was just for my specific example. I needed to redirect upon completion of the second action.

from redux-promise-middleware.

tomatau avatar tomatau commented on May 18, 2024

@rkoberg

What is the benefit of appending .payload.promise?

The return from dispatch when passed a promise action is an action object with the promise available at payload.promise, this allows you to await for the promise to be resolved, following @baronnoraz question and code.

I tend to move my async calls into separate functions and avoid action creators depending on each other.

from redux-promise-middleware.

rkoberg avatar rkoberg commented on May 18, 2024

OK. I have setup the firebase functions to return a promise. authWithPassword resolves into authData first. Then, async getUserFromAuthData has an await inside of it (to another Promisified firebase function). It resolves and all the data is sent in the return to the reducer. In other words, I don't seem to need .payload.promise -- wondering if I am somehow doing something wrong that currently results in the correct result.

from redux-promise-middleware.

tomatau avatar tomatau commented on May 18, 2024

The example I posted was using actionCreator functions inside an actionCreator and awaiting on dispatch calls, check the OP for the API of the functions used in the example code I posted.

from redux-promise-middleware.

pburtchaell avatar pburtchaell commented on May 18, 2024

@rkoberg I am going to close this since we resolved the OP's question. Would you mind creating a new issue? I'm happy to help.

from redux-promise-middleware.

rkoberg avatar rkoberg commented on May 18, 2024

I think we are good.

from redux-promise-middleware.

artemjackson avatar artemjackson commented on May 18, 2024

One more example:

/*
 * redux-thunk + redux-promise-middleware + redux-actions
 */
const _createAndStart = createAction(CREATE_AND_START,
  async (name, { dispatch }) => {
    await dispatch(create(name));
    await dispatch(start());
    await dispatch(push('/start'));
  });

export const createAndStart = (name) =>
  (dispatch) => dispatch(_createAndStart(name, { dispatch }));

from redux-promise-middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.