Skip to content
Advertisement

How to return value in nested function React (navigator.geolocation.getCurrentPosition())

I want to add weather by geolocation into my React+Redux app. I found that I can get geolocation by this JS method navigator.geolocation.getCurrentPosition(success, error, [options]). I want to dispatch that to my Redux weatherSlice, but this method returns undefined by default so I can’t dispatch it by createAsyncThunk.

JavaScript

What is the best way to solve this problem?

Advertisement

Answer

The getCurrentPosition method probably only works on https or localhost.

The payloadCreator function of crateAsyncThunk should return a Promise, you can convert a callback type function to a promise.

In your code it would look like this:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement