instanceof Promise in loadable utility not reliable in all environments
#2498
Replies: 1 comment 5 replies
So, in your environment, |
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Uh oh!
There was an error while loading. Please reload this page.
Recently faced an issue using jotai in a project that used some kind of es6.promise shim/polyfill to provide backwards compatibility for old browser.
It turns out the
instanceof Promisehere:https://github.com/pmndrs/jotai/blob/v2.8.0/src/vanilla/utils/loadable.ts#L13
It seems it isn't a super reliable way to check - I replaced it with
typeof x.then === "function"instead, but I'm not sure that's the best way to check for a promise either.There's an interesting discussion here:
https://stackoverflow.com/a/28133130/1305699
I notice that
isPromiseLikeis used here, which works fine:jotai/src/react/useAtomValue.ts
Line 10 in 9ceaef6
It might be best to use that everywhere instead of
instanceof Promise, e.g.jotai/src/vanilla/utils/atomWithObservable.ts
Line 166 in 9ceaef6
jotai/src/vanilla/utils/unwrap.ts
Line 18 in 9ceaef6
jotai/src/vanilla/store.ts
Line 94 in 9ceaef6
jotai/src/vanilla/utils/atomWithObservable.ts
Line 166 in 9ceaef6
jotai/src/vanilla/utils/atomWithStorage.ts
Line 236 in 9ceaef6
I doubt this is a problem faced by many but it should be an easy fix and it was a real pain to work out what was wrong!
All reactions