Search code examples
node.jsreact-nativees6-promisemetro-bundler

Promise objects not working in React Native project


First, I thought firebase functions were broken. Then, I tried to make a simple function that returns Promise. I put that to top-level index.js.

const testPromise = param => {
  console.log('return promise', param);
  return new Promise((resolve, reject) => {
    console.log('resolve promise');
    resolve('derp');
  });
};

testPromise('hede')
  .then(d => {
    console.log('resolved');
  })
  .catch(e => {
    console.log('e', e);
  });

AppRegistry.registerComponent(appName, () => App);

Console output:

return promise hede
resolve promise

See, there is no 'resolved' log nor error log.

I tried to change nodejs versions with nvm but no luck. I tried v12.5.0, v12.18.2, v15.6.0, v10.16.3. I tried like nvm use 12.5 && npm start

I've tried to create a new react-native project and I copied everything. Now, it works as expected. It is a solution for me but I didn't mark the question as solved because there is still a mystery, I couldn't figure why it doesn't work on the existing project.


Solution

  • It seems that setting inlineRequires to false inside the metro.config.js file resolves the issue.

    module.exports = {
      transformer: {
        getTransformOptions: async () => ({
          transform: {
            experimentalImportSupport: false,
            inlineRequires: false,
          },
        }),
      },
    };
    

    After making that change the output is:

    return promise hede
    resolve promise
    resolved
    

    Source: https://github.com/facebook/react-native/issues/31558#issuecomment-878342213