Axios GET 请求不会在 while 循环中执行
Axios GET Request won't execute in a while loop
当我 运行 node index.js
使用此代码时,一切正常。它所做的是使用 randomGeo 函数获取随机坐标,然后使用 Axios 调用将其转换为地址。最后,此 transPredtoJSON 函数在 Axios 调用的 .then 中调用,它获取地址并将其添加到 JSON 文件中。当我想多次执行此操作时会出现问题,我将在下一个代码片段中显示。
randomCoordinate = randomGeo({ latitude: originlat, longitude: originlng }, radius)
lat = randomCoordinate.latitude
lng = randomCoordinate.longitude
reverseGeoCodeURL = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${GEOCODE_API_KEY}`
// get closest address to the coordinate
axios.get(reverseGeoCodeURL).then(res => {
let ranAddr = res.data.results[0].formatted_address
let correctCity = /, Southlake,/i
if (correctCity.exec(ranAddr)) { // only Southlake addresses
transPredtoJSON(ranAddr) // are added to the output json file
}
}).catch(error => {
console.error('error: ', error);
})
下面我将上面的代码包装在一个 while 循环中。我将有一个计数器来跟踪有多少地址被添加到输出文件并在达到限制时停止。此外,axios.get 永远不会被调用,因为控制台日志只会无限输出 count: 0
。奇怪的是,当我在 axios 调用之后立即移动 count += 1
以便无论是否添加地址计数都会增加,axios.get 被调用并将地址添加到文件中,就像它应该的那样。为什么 Axios 运行 不在这个 while 循环中?
let enoughAddresses = 10
let count = 0
while (count < enoughAddresses) {
console.log('count', count);
randomCoordinate = randomGeo({ latitude: originlat, longitude: originlng }, radius)
lat = randomCoordinate.latitude
lng = randomCoordinate.longitude
reverseGeoCodeURL = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${key}`
axios.get(reverseGeoCodeURL).then(res => {
console.log('inside axios .then');
let ranAddr = res.data.results[0].formatted_address
let corrCity = /, Southlake,/i
if (corrCity.exec(ranAddr)) {
transPredtoJSON(ranAddr)
count += 1 // only increment count when an address has been added
console.log('count', count);
}
}).catch(error => {
console.error('error: ', error);
})
}
问题是因为 axios 函数是 运行 异步的。您或许可以使用 promise 来解决这个问题。
此伪代码可能会为您指明正确的方向:
let enoughAddresses = 10
for (i = 0; i < enoughAddresses; i++) {
addresses.push(new Promise((r, j) => {
// your async function to get addresses
});
}
// returns a promise when all your async functions are done
Promise.all(addresses).then((values) => {
// values should contain your addresses
});
另外,this供参考。
当我 运行 node index.js
使用此代码时,一切正常。它所做的是使用 randomGeo 函数获取随机坐标,然后使用 Axios 调用将其转换为地址。最后,此 transPredtoJSON 函数在 Axios 调用的 .then 中调用,它获取地址并将其添加到 JSON 文件中。当我想多次执行此操作时会出现问题,我将在下一个代码片段中显示。
randomCoordinate = randomGeo({ latitude: originlat, longitude: originlng }, radius)
lat = randomCoordinate.latitude
lng = randomCoordinate.longitude
reverseGeoCodeURL = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${GEOCODE_API_KEY}`
// get closest address to the coordinate
axios.get(reverseGeoCodeURL).then(res => {
let ranAddr = res.data.results[0].formatted_address
let correctCity = /, Southlake,/i
if (correctCity.exec(ranAddr)) { // only Southlake addresses
transPredtoJSON(ranAddr) // are added to the output json file
}
}).catch(error => {
console.error('error: ', error);
})
下面我将上面的代码包装在一个 while 循环中。我将有一个计数器来跟踪有多少地址被添加到输出文件并在达到限制时停止。此外,axios.get 永远不会被调用,因为控制台日志只会无限输出 count: 0
。奇怪的是,当我在 axios 调用之后立即移动 count += 1
以便无论是否添加地址计数都会增加,axios.get 被调用并将地址添加到文件中,就像它应该的那样。为什么 Axios 运行 不在这个 while 循环中?
let enoughAddresses = 10
let count = 0
while (count < enoughAddresses) {
console.log('count', count);
randomCoordinate = randomGeo({ latitude: originlat, longitude: originlng }, radius)
lat = randomCoordinate.latitude
lng = randomCoordinate.longitude
reverseGeoCodeURL = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${key}`
axios.get(reverseGeoCodeURL).then(res => {
console.log('inside axios .then');
let ranAddr = res.data.results[0].formatted_address
let corrCity = /, Southlake,/i
if (corrCity.exec(ranAddr)) {
transPredtoJSON(ranAddr)
count += 1 // only increment count when an address has been added
console.log('count', count);
}
}).catch(error => {
console.error('error: ', error);
})
}
问题是因为 axios 函数是 运行 异步的。您或许可以使用 promise 来解决这个问题。
此伪代码可能会为您指明正确的方向:
let enoughAddresses = 10
for (i = 0; i < enoughAddresses; i++) {
addresses.push(new Promise((r, j) => {
// your async function to get addresses
});
}
// returns a promise when all your async functions are done
Promise.all(addresses).then((values) => {
// values should contain your addresses
});
另外,this供参考。