Skip to content

Reactions with async functions #152

@dArignac

Description

@dArignac

Hey there,

first of all: thanks for the lib, I love it!

I'm using the store in a plain class that reacts to store updates and on update I want to execute async code and update the store.
Usually one would do that with a reaction:

this.storeSubscription = MyStore.createReaction(
      (s) => s.someAttribute,
      (watched, draft, original, lastWatched) => { <some code> }
    )

As we cannot have awaits in the reaction function, the async code is in a class method async doAsyncStuff(watched, draft, original, lastWatched). Thus I'd like to bind it:

this.storeSubscription = MyStore.createReaction(
      (s) => s.someAttribute,
      this.doAsyncStuff.bind(this)
    )

However this fails with an immer exception: [Error] Unhandled Promise Rejection: Error: [Immer] produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '[object Promise]'.

Is there a way to use the reactions with async functions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions