Question to snapshot.getPromise vs snapshot.getLoadable #2012
-
Hi team! Currently, I am confused with the The way I am using them is quite the same, for example const useSomeCallback = () => {
return useRecoilCallback(() => async ({ snapshot }) => {
// use getPromise
const aSnapshotFromPromise = await snapshot.getPromise(someAtomOrSelector)
// use getLoadable
const aSnapshotFromLoadable = snapshot.getLoadable(someAtomOrSelector).getValue()
// some business logic...
})
} In most cases, both two work but sometimes it may different, sometimes I am wondering what's the main difference between these two methods and when I should consider using Thanks in advance for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
As you point out they are very similar. |
Beta Was this translation helpful? Give feedback.
As you point out they are very similar.
getLoadable()
is synchronous and returns aLoadable
object which encodes the state and either the value, error, or promise to an async value.getPromise()
on the other hand returns an async promise to the value, even if the value is synchronous or rejected to the error. You may wish to use thegetLoadable()
version if you want to synchronously handle the result, error, or pending state.