Santiago Bendavid Santiago Bendavid -4 years ago 135
Javascript Question

Is it possible to use async / await in React JS?

I started programming in React Native, and I got used to use the syntax:

async myFunction(){
...
return await otherFunction();
}


But I don't know how to make it compatible with React JS and React Native in a shared package. How can I accomplish this so that it works in both platforms?

Thanks!

Answer Source

React Native ships with Babel and some Babel presets, whereas React on the web is just React related code.

If you want to use async/await on the web today you'll need Babel and the correct transforms: https://babeljs.io/docs/plugins/transform-async-to-generator/

or the stage-1 presets, which is fairly common in React apps today. http://babeljs.io/docs/plugins/preset-stage-1/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download