17

Is it already possible to use top-level await in react-native? I saw it's been added to TypeScript 3.8 (I was not using TS, I just tried to see if it would help but no). However in react-native file transform is handled by babel and I don't think the current metro preset handles top-level awaits. Is there any way to make it work?

Alexis
  • 16,629
  • 17
  • 62
  • 107

1 Answers1

1

ECMAScript feature top-level await stands on two conditions:

  1. ES2017+
  2. ECMAScript native modules (they are compiled to functions)

But React-Native uses node modules, so the answer is no.

See Ecma international standard for more

EDIT:

According to comment and more is possible in Node.js. But React-Native uses V8, which supports it too. It just compiles JSX to C++.

Tomáš Wróbel
  • 658
  • 3
  • 12
  • 1
    It seems there is node support now. https://www.stefanjudis.com/today-i-learned/top-level-await-is-available-in-node-js-modules/ But I can't figure out how to get it working with react native. Would you mind editing your answer to show how if you know? – Qrow Saki Aug 20 '21 at 20:02
  • @QrowSaki Did you ever find out how to enable it? I can't get it to work in a stock react native app at all. – bleeckerj Mar 10 '22 at 00:08
  • @bleeckerj I think you need dynamic import("..") to make module a promise – Tomáš Wróbel Mar 11 '22 at 16:16
  • @TomášWróbel Thank you — can you elaborate or provide a pointer to a more detailed explanation? I'm still mystified. Although I can get it to work in one configuration that's quite simple, but not other similar ones. I don't know what is "turning it on" at all. (cf https://github.com/fireship-io/web3-nft-dapp-tutorial/blob/main/package.json) – bleeckerj Mar 12 '22 at 17:11