从函数(作为参数)返回值到外部函数Javascript

时间:2019-02-23 14:35:59

标签: javascript node.js function return-value

这是我的代码段

function getDistance(){
var origins = ['San Francisco CA'];
var destinations = ['New York NY'];

distance.matrix(origins, destinations, function (err, distances) {
    if (distances.status == 'OK') {
        var origin = distances.origin_addresses[0];
        var destination = distances.destination_addresses[0];
        if (distances.rows[0].elements[0].status == 'OK') {
            var distance = distances.rows[0].elements[0].distance.text;
            return distance;
        }else{
            return 0;
        } 
    } else{return 0;} 
});

我想在调用getDistance()函数时返回距离值

我该怎么做?

0 个答案:

没有答案