facebookexperimental / Recoil

Recoil is an experimental state management library for React apps. It provides several capabilities that are difficult to achieve with React alone, while being compatible with the newest features of React.
https://recoiljs.org/
MIT License
19.5k stars 1.18k forks source link

getPromise() in atom effects does not resolve #2314

Open phjardas opened 2 months ago

phjardas commented 2 months ago

Consider the following scenario where an atom effect tries to get the value of another atom.

const oneAtom = atom({
  key: "one",
  effects: [
    ({ setSelf }) => {
      setTimeout(() => setSelf("ok"), 1000);
    },
  ],
});

const twoAtom = atom({
  key: "two",
  effects: [
    ({ getPromise, setSelf }) => {
      getPromise(oneAtom)
        .then((value) => setSelf(value))
        .catch((error) => console.error("error in two:", error));
    },
  ],
});

The atom one starts in loading state and only resolves after a second.

I'd expect the promise returned from getPromise(oneAtom) to be initially pending (which it does) and eventually resolve with the value of the atom one when that one resolves. However, the returned promise remains in pending state forever; neither the then nor the catch callback are ever called.

Note that I've intentionally omitted the cleanup logic for setTimeout to keep the example concise.