从未定义的 distancematrix 回调函数返回值?
Returning a value from distancematrix callback function undefined?
我假设是因为它是异步的。这是我的代码:
service.getDistanceMatrix(
{
origins: [yourPosition],
destinations: [end],
travelMode: 'WALKING',
}, callback);
function callback(response, status) {
if (status == 'OK') {
var origins = response.originAddresses;
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
var element = results[j];
var distance = element.distance.value;
var duration = element.duration.value;
}
}
}
我想要做的是return回调中的距离和持续时间值。然后,return 该值,并将其提供给调用我上面代码的父函数的函数。我的蛮力想法是从回调内部 return,然后 return 从回调函数外部接收值,因此我可以实际使用该值。为了清楚起见,据我所知,这是我的功能的图表:
get_avg_speed
get_distance
callback
我知道回调是异步的,有正确的方法吗?
Promise 是你的朋友。这是使用 Promise 构造函数的代码。像这样的二进制回调很麻烦,所以我把 response
和 status
放在一个对象中。
const promisifiedGetDistanceMatrix = params => new Promise(resolve => {
service.getDistanceMatrix(params, (response, status) => {
resolve({ response, status })
})
})
const getElementsDistanceDuration = ({ response, status }) => {
if (status !== 'OK') return []
let elements = []
let origins = response.originAddresses;
for (let i = 0; i < origins.length; i++) {
let results = response.rows[i].elements;
for (let j = 0; j < results.length; j++) {
let element = results[j];
let distance = element.distance.value;
let duration = element.duration.value;
elements.push({ distance, duration })
}
}
return elements
}
然后你可以像这样使用这些 promise 返回函数
const get_avg_speed = async (...args) => {
/* ... */
const elementsDistanceDuration = await promisifiedGetDistanceMatrix({
origins: [yourPosition],
destinations: [end],
travelMode: 'WALKING',
}).then(getElementsDistanceDuration)
}
我假设是因为它是异步的。这是我的代码:
service.getDistanceMatrix(
{
origins: [yourPosition],
destinations: [end],
travelMode: 'WALKING',
}, callback);
function callback(response, status) {
if (status == 'OK') {
var origins = response.originAddresses;
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
var element = results[j];
var distance = element.distance.value;
var duration = element.duration.value;
}
}
}
我想要做的是return回调中的距离和持续时间值。然后,return 该值,并将其提供给调用我上面代码的父函数的函数。我的蛮力想法是从回调内部 return,然后 return 从回调函数外部接收值,因此我可以实际使用该值。为了清楚起见,据我所知,这是我的功能的图表:
get_avg_speed
get_distance
callback
我知道回调是异步的,有正确的方法吗?
Promise 是你的朋友。这是使用 Promise 构造函数的代码。像这样的二进制回调很麻烦,所以我把 response
和 status
放在一个对象中。
const promisifiedGetDistanceMatrix = params => new Promise(resolve => {
service.getDistanceMatrix(params, (response, status) => {
resolve({ response, status })
})
})
const getElementsDistanceDuration = ({ response, status }) => {
if (status !== 'OK') return []
let elements = []
let origins = response.originAddresses;
for (let i = 0; i < origins.length; i++) {
let results = response.rows[i].elements;
for (let j = 0; j < results.length; j++) {
let element = results[j];
let distance = element.distance.value;
let duration = element.duration.value;
elements.push({ distance, duration })
}
}
return elements
}
然后你可以像这样使用这些 promise 返回函数
const get_avg_speed = async (...args) => {
/* ... */
const elementsDistanceDuration = await promisifiedGetDistanceMatrix({
origins: [yourPosition],
destinations: [end],
travelMode: 'WALKING',
}).then(getElementsDistanceDuration)
}