How does WeChat Mini Programs get the parameter return returned by the callback function successfully called by the API?

I want to write a public function that gets the location of the user. I currently write that the data of return is not assigned a value because of asynchronous requests, so printing locationData should be undefined. Would like to ask the gods how to write to return the data returned in the success?

< hr >

public.js:

var QQMapWX = require("qqmap-wx-jssdk.js"); //
var qqmapsdk = new QQMapWX({ key: "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" });

function getLocation() {
    var data;
    wx.getLocation({
        type: "wgs84",
        success: function (res) {
            let latitude = res.latitude;
            let longitude = res.longitude;
            qqmapsdk.reverseGeocoder({
                location: {
                    latitude: latitude,
                    longitude: longitude
                },
                success: function (res) {
                    data = res.result;
                }
            });
        },
    });
 
    return data;
}

index.js:

var publicJS = require("../utils/public.js");
 
locationSelect: function() {
    var locationData = publicJS.getLocation();
    console.log(locationData);
}

use Promise

function getLocation() {
    return new Promise(function(resolve, reject){
         wx.getLocation({
            type: 'wgs84',
            success: function (res) {
                let latitude = res.latitude;
                let longitude = res.longitude;
                qqmapsdk.reverseGeocoder({
                    location: {
                        latitude: latitude,
                        longitude: longitude
                    },
                    success: function (res) {
                        resolve(res.result);
                    }
                });
            },
        });
    })
   

}

index:js

var publicJS = require('../utils/public.js');
 
locationSelect: function() { 
    publicJS.getLocation().then(function(locationData){
        console.log(locationData);
    })
}
Menu