Giter Site home page Giter Site logo

Comments (4)

igorbarbashin avatar igorbarbashin commented on June 10, 2024 1

I also have problems figuring out how to make async/await working properly in effects.

It only works if I make the first function async:

getPosts: async (effects) => {
      const result = await fetch('https://jsonplaceholder.typicode.com/posts')
      const posts = await result.json()
    
      return state => ({ ...state, posts })
    }

But I haven't figured out how do I access state from inside of the effect.
Placing async anywhere else screws up the state

from freactal.

michelmattos avatar michelmattos commented on June 10, 2024 1

Ah, I misunderstood you, @igorbarbashin. You're right. The first function needs to be async.
It is because the effect signature can be Effects => State => State or Effects => Promise<State => State>. Since an async function is the same as () => Promise<any> then it fufill the latter case.
With that, my example can be rewritten like this:

const wrapComponentWithState = provideState({
  initialState: () => ({
    posts: null,
    postsPending: false
  }),
  effects: {
    setPostsPending: update((state, postsPending) => ({ postsPending })),
    setPosts: update((state, posts) => ({ posts })),
    getPosts: async effects => {
      await effects.setPostsPending(true)
      const result = await fetch('https://jsonplaceholder.typicode.com/posts')
      const posts = await result.json()
      await effects.setPostsPending(false)
      return state => ({ ...state, posts })
    }
  }
})

from freactal.

bingomanatee avatar bingomanatee commented on June 10, 2024

As far as I can tell that is a bit of a a "dead zone" in effects. They can change and be aware of state in the final (state) => newState method.

There is a "hackish" way of doing this in two stages. it takes advantage of the fact that the final lambda (state) => newState) has acces to the effects from the outer closure AND the state from its own parameter:

effectA [ (effects, param) => ... ]
effectB    [
        (effects ?) => 
         [
          (state) => 
            effects.effectA(state.foo)
           return state (unchanged)
       ]
 ]

i.e., effectB gets a state value and calls effectA with it but does NOT return the promise resulting from the effectA call. It then returns state in whatever state its in.

This is a very jury-rigged way of having an effect pull a parameter from state but it will work.
Personally I'm fine with having states take parameters that I manually extract from state, as wherever I call an effect from will also have access to state.

from freactal.

michelmattos avatar michelmattos commented on June 10, 2024

@igorbarbashin It's probably because the foremost function can't return a promise but another function (the latter one can be async)

from freactal.

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.