Agontuk / react-native-geolocation-service

React native geolocation service for iOS and android
https://www.npmjs.com/package/react-native-geolocation-service
MIT License
1.6k stars 290 forks source link

getCurrentPosition returns immediately with undefined instead of result from onSuccess argument #341

Closed rafabulsing closed 2 years ago

rafabulsing commented 2 years ago

Environment

System:
    OS: macOS 12.0.1
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
    Memory: 24.38 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.4 - /usr/local/bin/node
    Yarn: Not Found
    npm: 8.5.3 - /usr/local/bin/npm
    Watchman: 2021.08.30.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /Users/rafael/.rbenv/shims/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 15.4, macOS 12.3, tvOS 15.4, watchOS 8.5
    Android SDK:
      API Levels: 28, 29, 30, 31
      Build Tools: 28.0.3, 29.0.2, 30.0.2, 31.0.0
      System Images: android-29 | Intel x86 Atom_64, android-29 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 2020.3 AI-203.7717.56.2031.7583922
    Xcode: 13.3/13E113 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_292 - /usr/bin/javac
    Python: 3.9.6 - /Users/rafael/.pyenv/shims/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1 
    react-native: 0.63.4 => 0.63.4 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Platforms

Is this issue related to Android, iOS, or both ? Both

Versions

Description

Instead of returning result from onSuccess param, getCurrentPosition returns immediately with undefined, and callback is called afterwards.

Reproducible Demo

const test = async () => {
    console.log("Starting test");
    const result = await getCurrentPosition(
        (position) => {
            console.log("Got position");
            return true;
        },
    );
    console.log("Finishing test, result:", result);
);

Expected Results

Expected logs:

Starting test
Got position
Finishing test, result: true

Actual logs:

Starting test
Finishing test, result: undefined
Got position
lucioreyli commented 2 years ago

We have 2 problems:

rafabulsing commented 2 years ago

Actually, getCurrentPosition is async (returns a Promise) in the version I have (2.0.2). At least that's what index.d.ts is describes it as. Lines 61-65:

  export function getCurrentPosition(
    successCallback: SuccessCallback,
    errorCallback?: ErrorCallback,
    options?: GeoOptions
  ): Promise<GeoPosition>

In any case, I decided to do a workaround to get it to work as I need it to. Here it is, in case anyone is in the same situation:

const getLocation = (onSuccess, onError, options) => new Promise((resolve, reject) => {
  Geolocation.getCurrentPosition(
    (result) => resolve(onSuccess(result)),
    (error) => reject(onError(error)),
    options,
  );
});
Agontuk commented 2 years ago

getCurrentPosition is not an async function, it was a typo in the index.d.ts. If possible, try to update to the latest version. You are using a pretty old version.

GSFZamai commented 2 years ago

getCurrentPosition is not an async function, it was a typo in the index.d.ts. If possible, try to update to the latest version. You are using a pretty old version.

You're right, in my case, at index.d.ts the getCurrentPosition's return is actually typed as void, but I'm receiving this warning at my console: Possible Unhandled Promise Rejection (id: 0): TypeError: Cannot read property 'getCurrentPosition' of null

Edit: I reinstalled the debug app and worked.

Agontuk commented 2 years ago

Closing this as latest version contains the proper typings.