Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Native 0.63 introduces Promise.allSettled but don't work #30236

Closed
douglasjunior opened this issue Oct 23, 2020 · 9 comments
Closed

React Native 0.63 introduces Promise.allSettled but don't work #30236

douglasjunior opened this issue Oct 23, 2020 · 9 comments
Labels

Comments

@douglasjunior
Copy link

douglasjunior commented Oct 23, 2020

React Native 0.63 introduces Promise.allSettled but don't work.

Description

Until version 0.61 we have used the Promise.allSettled polyfill, but in React Native 63 it stopped working because the Promise.allSettled is a valid function and polyfill is ignored.

However, the native Promise.allSettled don't work as expected.

React Native version:

System:
    OS: macOS 10.15.7
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
    Memory: 136.63 MB / 8.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.19.0 - ~/.nvm/versions/node/v12.19.0/bin/node
    Yarn: 1.22.10 - ~/.nvm/versions/node/v12.19.0/bin/yarn
    npm: 6.14.8 - ~/.nvm/versions/node/v12.19.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.9.3 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.1, DriverKit 19.0, macOS 10.15, tvOS 14.0, watchOS 7.0
    Android SDK:
      API Levels: 23, 26, 27, 28, 29
      Build Tools: 23.0.1, 23.0.3, 24.0.3, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.3, 28.0.0, 28.0.0, 28.0.1, 28.0.2, 28.0.3, 29.0.0, 29.0.2, 30.0.1
      System Images: android-29 | Google Play Intel x86 Atom
      Android NDK: 20.1.5948944
  IDEs:
    Android Studio: 4.0 AI-193.6911.18.40.6626763
    Xcode: 12.1/12A7403 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_181 - /usr/bin/javac
    Python: 3.8.2 - /Users/douglas/.pyenv/shims/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1 
    react-native: 0.63.3 => 0.63.3 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps To Reproduce

  1. Run any code like that:
const successReq = (wait) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(new Date());
    }, wait);
  });
};

const errorReq = (wait) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      reject(new Date());
    }, wait);
  });
};

const makeRequest = async () => {
  const result = await allSettled([
    successReq(500),
    successReq(1000),
    errorReq(600),
    successReq(1500)
  ]);

  console.log({ result });
};

makeRequest();
  1. See the buggy result in console:
{
    "result": [
        {
            "_bitField": 33554432,
            "_settledValueField": 2020-10-23T18: 43: 47.045Z
        },
        {
            "_bitField": 33554432,
            "_settledValueField": 2020-10-23T18: 43: 47.543Z
        },
        {
            "_bitField": 16777216,
            "_settledValueField": 2020-10-23T18: 43: 47.143Z
        },
        {
            "_bitField": 33554432,
            "_settledValueField": 2020-10-23T18: 43: 48.219Z
        }
    ]
}

Expected Results

{
  "result": [
    {
      "status": "fulfilled",
      "value": "2020-10-23T18:52:51.836Z"
    },
    {
      "status": "fulfilled",
      "value": "2020-10-23T18:52:52.336Z"
    },
    {
      "status": "rejected",
      "reason": "2020-10-23T18:52:51.936Z"
    },
    {
      "status": "fulfilled",
      "value": "2020-10-23T18:52:52.837Z"
    }
  ]
} 

Snack, code example, screenshot, or link to a repository:

I don't think that is necessary.

@douglasjunior douglasjunior changed the title React Native 0.63 introduces Promise.allSettled but dont works React Native 0.63 introduces Promise.allSettled but don't work Oct 23, 2020
@ravirajn22
Copy link

From logging on iOS (real device without turning on remote debugging) using react-native v0.63, Promise.allSettled is undefined. So it's not supported.

But from the internals what I see is on android if you use Hermes engine then this Promise Object comes from it, instead of using a polyfill. May be Hermes engine might have a wrong implementation of Promise.allSettled. If you are using Hermes try disabling it for debugging (I have not used).

If you have any other details post here.

@douglasjunior
Copy link
Author

Good point. We dont use Hermes, but maybe we are using in debug mode.

I will check, thanks!

@douglasjunior
Copy link
Author

Solved, the problem happened because we used another promises polyfill (Bluebird) that ended up causing the problem.

Thank you for your help!

@Overtorment
Copy link

so, is Promise.allSettled supported in RN ?

@ahartzog
Copy link

Promise.allSettled is undefined in React Native 0.64.1 for me. Using Hermes for both iOS and Android now.

@douglasjunior
Copy link
Author

RN dont have allSettled by default, you need to use Promise.allSettled polyfill.

@pktippa
Copy link

pktippa commented Jul 9, 2021

you can implement allSettled by yourself.

const allSettled = (promises) => {
  return Promise.all(promises.map(promise => promise
      .then(value => ({ state: 'fulfilled', value }))
      .catch(reason => ({ state: 'rejected', reason }))
  ));
}

@pistonsky
Copy link

const allSettled = (promises) => {
  return Promise.all(promises.map(promise => promise
      .then(value => ({ status: 'fulfilled', value }))
      .catch(reason => ({ status: 'rejected', reason }))
  ));
}

@retyui
Copy link
Contributor

retyui commented Oct 9, 2021

under the hood react-native uses promise modules to polyfill Promise

'use strict';
const Promise = require('promise/setimmediate/es6-extensions');
require('promise/setimmediate/done');
require('promise/setimmediate/finally');

So if in that module will implemented allSettled it can be easily shipped to react-native


UPD: I made PR: then/promise#171

@facebook facebook locked as resolved and limited conversation to collaborators Nov 4, 2021
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Nov 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

9 participants