pianoman102
pianoman102

Reputation: 549

Adapting react-native code to use promises

I'm learning React Native and I was looking up how to make a promise chain with an API call as referenced here: https://facebook.github.io/react-native/docs/network.html#handling-the-response. In looking how to get user permissions, I looked at the docs page for it (https://facebook.github.io/react-native/docs/permissionsandroid.html) and wondered if the promise concept could be applied here to make it a little

My main question is this: How does promises improve functionality (if at all) and what is the best way to adapt code to use promises?

Here is some other code that I would use for easy reference:

async requestLocationPermission() {
    const chckLocationPermission = PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
        if (chckLocationPermission === PermissionsAndroid.RESULTS.GRANTED) {
            console.log("You've access for the location");
        } else {
            try {
                const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
                    {
                        'title': 'This App required Location permission',
                        'message': 'We required Location permission in order to get device location ' +
                            'Please grant us.'
                    }
                )
                if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                    console.log("You've access for the location");
                } else {
                    console.log("You don't have access for the location");
                }
            } catch (err) {
                console.log(err)
            }
        }
    };

Upvotes: 0

Views: 206

Answers (2)

cameck
cameck

Reputation: 2098

The short answer is yes.
If you need the data from your API request, then you need to use promises. For more on why you would need to use a promise, you want to learn about more about the call stack.

I think there's a small confusion (which is pretty common), but the code you linked is also using promises.
Async/Await is a new JavaScript feature that makes writing promises easier.

There are a more than a few ways you can write promises in JavaScript e.g. Fetch, Promise, Async/Await. Not to mention libraries like AngularJS have their own implementation of this.
I'd recommend to study up on the Call Stack first and then go from there.

Upvotes: 1

suchcodemuchwow
suchcodemuchwow

Reputation: 1046

Simply:

  • You are writing much cleaner code with async await rather than then() catch() etc. methods
  • If you have other programming language knowledge probably you miss try/catch blocks. It allows you write sync & async methods in same block.

Personal recommendation you should be familiar with callbacks => promises => generators => async await (learn in order). It's actually your decision to use which one best fits for your purposes. I'd suggest to look very detailed guide about these topics (he actually wrote a book for just these topics xd) You Don't Know JS: Async & Performance

Upvotes: 0

Related Questions