如何将承诺链的结果导出到单独的模块?

How do I export the result of a promise chain to a separate module?

我想在 index.js 中使用 googleApi.js 的结果,但无法从 index.js 访问数据。根据我尝试过的解决方案,结果要么是 undefined 要么是一个 promise 对象。有人能给我指出正确的方向吗?

index.js:

const googleClient = require('./googleApi.js')

let one = '52.4362,4.7291'
let two = '52.2672,5.0813'

const test = async() => {
    const result = await googleClient.getTimeAndDistance(one,two)
    console.log(result)
}

test()

googleApi.js

const googleMapsClient = require('@google/maps').createClient({
    key: 'MY_API_KEY',
    Promise: Promise
})

getTimeAndDistance = (origin, destination) => {
    googleMapsClient.directions({
        origin: origin,
        destination: destination,
        language: 'en',
        units: 'metric',
    })
        .asPromise()
        .then((res) => {
            return res.json.routes[0].legs[0]
        })
        .catch((err) => {
            console.log(err)
        })
}

module.exports.getTimeAndDistance = getTimeAndDistance

googleApi.js 中的代码有效,我能够将结果记录到控制台,但即使导出函数,我也无法访问模块外部的数据。

index.jsreturngoogleMapsClientgetTimeAndDistance里面制作在 getTimeAndDistance.

之外可用 res.json.routes[0].legs[0]
const googleMapsClient = require('@google/maps').createClient({
    key: 'MY_API_KEY',
    Promise: Promise
})

getTimeAndDistance = (origin, destination) => {
 return googleMapsClient.directions({
//^^^^^ 
        origin: origin,
        destination: destination,
        language: 'en',
        units: 'metric',
        })
        .asPromise()
        .then((res) => {
            return res.json.routes[0].legs[0]
        })
        .catch((err) => {
            console.log(err)
        })
}

module.exports.getTimeAndDistance = getTimeAndDistance