Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 29721

How do I resolve a Promise inside a Task?

$
0
0

I have a method wich should get a location and send it's result as a resolved Promise. However I have no idea how to await for the listener to finish or to workaround. It uses Google Services API to get current location.

// LocationModule.java
@ReactMethod
public void getLocation(String debugMessage, Promise promise) {
    try {
        this.fusedLocationClient.getLastLocation()
            .addOnSuccessListener(new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    if(location != null) {
                        promise.resolve(location);
                    }
                }
            });
        // promise.resolve(debugMessage); this would be called before the promise above and if it isn't present here app crashes
    } catch(Exception e) {
        promise.reject("Something went wrong", e);
    }
}

Viewing all articles
Browse latest Browse all 29721

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>