12

I planned updating some Promise.all to Promise.allSettled in my React Native - Expo Project but the function does not Exist. i checked all Versions and everything fits but i still cant use the function.

node -v: 14.15.4

expo SDK -v version: ^40.0.1

expo uses react native -v 0.63 in their SDK version. This should not be the problem. This is the error message:

Promise.allSettled is not a function

Does anybody knows a solution to this? Thank you for your help!

jovan
  • 227
  • 1
  • 2
  • 11
  • I m not sure about what a polyfill is. i googled it right now and i think expo already use one. Its called babel – jovan Mar 11 '21 at 02:36
  • Babel alone is fundamentally a transpiler, not a polyfill service, though you can use polyfills with it with @babel/polyfill – CertainPerformance Mar 11 '21 at 02:46
  • Promise.allSettled is a relatively new API so you may need to polyfill it if you want to use it. Expo and React Native use JavaScriptCore by default. On iOS, they will use the version of JavaScriptCore that is available for that operating system version. On Android, it is provided by this package: https://github.com/react-native-community/jsc-android-buildscripts. This is a long winded way of saying that what works in your web browser or Node may not work in the React Native environment and vice versa because they use different JS engines and versions of those engines. – brentvatne Mar 11 '21 at 02:55
  • @jovan it was updated in 0.70.6 version of RN and works from the box, check my answer https://stackoverflow.com/questions/66575667/promise-allsettled-is-not-a-function/74458309#74458309 – Kirill Novikov Jan 16 '23 at 10:37

3 Answers3

13

For anyone coming across this issue, apparently this was fixed in v64:

https://github.com/facebook/react-native/issues/30236#issuecomment-939286987

For older versions, you can use a simple polyfill:

Promise.allSettled = Promise.allSettled || ((promises) => Promise.all(
    promises.map(p => p
        .then(value => ({
            status: "fulfilled",
            value
        }))
        .catch(reason => ({
            status: "rejected",
            reason
        }))
    )
));
NullDev
  • 6,739
  • 4
  • 30
  • 54
8

React Native uses https://github.com/then/promise for Promises and only in version 8.2.0 it was added with allSettled. React Ntaive team updated to this version only in 0.70.6 (here list of changes including promise version bumping to 8.3.0 https://github.com/facebook/react-native/releases/tag/v0.70.6)

Kirill Novikov
  • 2,576
  • 4
  • 20
  • 33
3
export const PromiseHelperAllSettled = (promises) => {
    return Promise.all(promises.map(function (promise) {
        return promise.then(function (value) {
            return { state: 'fulfilled', value: value };
        }).catch(function (reason) {
            return { state: 'rejected', reason: reason };
        });
    }));
};

import { PromiseHelperAllSettled } from './PromiseHelperAllSettled';
useEffect(() => {
if (Promise && !Promise.allSettled) {
      Promise.allSettled = PromiseHelperAllSettled;
    }
},[]);
Thaiyalnayaki
  • 148
  • 1
  • 8