javascript - 为什么我需要使用回调函数来访问 Web api 的变量?
问题描述
const request = require('postman-request');
const geocode = (address) => {
const url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${address}.json?access_token=pk.eyJ1IjoibWVya3VyMTIzIiwiYSI6ImNrYjVndDk3bjBvNGEyeW16cHlid2txZ3YifQ.NGOWOq0yq0wvkhzDzjnUpQ&limit=1`;
request({ url, json: true }, (error, response) => {
const data = response.body;
if (error) {
return 1;
} else if (data.message === 'Not Found' || data.features.length === 0) {
return 1;
} else {
return {
longitude: data.features[0].center[0],
latitude: data.features[0].center[1],
location: data.features[0].place_name,
};
}
});
};
output = geocode("New York");
console.log(typeof output);
大家好,我知道代码不起作用,我应该使用回调函数,但我想知道为什么会这样。为什么我不能返回结果,在这种情况下是一个对象,并像往常一样访问它?
感谢您的每一个有用的回复!
解决方案
推荐阅读
- android - SQLite 或文本文件可以更快地在 Android 中存储传感器数据吗?
- java - SAML SSO 与基于 Struts 2 的应用程序集成,使其作为服务提供者工作
- javascript - 使用 swal 删除对 sql 的操作
- android - 替换 Intent 数据中的 char
- unity3d - Unity - 粒子边界与粒子大小不匹配
- swift - 返回集合的切片类型
- python - 通过标签进行 Django 管理员搜索
- swagger - 如何从 swagger 2.0 swagger-codegen-cli.jar 生成 java 代码
- python - 如何在python中获取由于系统时钟回退而不会倒退的当前unix纪元时间
- python - 将函数 (tf.square()) 应用于张量的某些值 - TensorFlow