javascript - javascript 获取 api 并返回数据以使用它
问题描述
我已经在 javascript 函数中有一个问题axios 来返回有关此的数据,但它已被关闭,我仍在与此作斗争。
但是,我取得了一些进展。
class Api {
getPin = () => {
fetch("https://jsonplaceholder.typicode.com/posts/1")
.then((response) => response.json())
.then((json) => this.getResults(json));
};
async getResults(data) {
let pin = await data;
console.log(pin.id);
}
}
let api = new Api();
api.getPin();
这将获得 API 调用,我可以对其进行控制台记录(这是在 10 小时后查看此内容:'-() 理想情况下,我想在应用程序的其他地方使用结果
有人可以帮忙吗?我正在为此苦苦挣扎
编辑:
class Api {
getPin = () => {
fetch("https://jsonplaceholder.typicode.com/posts/1")
.then((response) => response.json())
.then((json) => this.getResults(json));
};
getResults(data) {
// console.log(data);
return data;
}
}
let api = new Api();
let pin = api.getPin();
console.log(pin);
如果我摆脱异步并等待并从函数返回数据,我在尝试控制台日志时会变得不确定。我还缺少什么?
解决方案
推荐阅读
- jira - 如何获取未链接到测试用例的用户故事?有什么查询吗?我已经尝试过链接问题
- java - 如何使用 CIFS 在 Android Studio 中访问文件
- java - 配置 Springboot Cloud Config Server - management.security.enabled=false flag 不工作
- bash - 在 Bash 中打破循环
- openssl - 生成 rsa 公钥的哈希值
- swift - 使用调度组时关闭丢失
- java - 我应该如何更改此 Shuffle 方法以使 shuffle 功能正常工作
- excel - VBA中的总和标签值
- android-studio - Android studio 3.4 布局编辑器不允许在设计器中为 ConstraintLayout 操作元素
- php - 我尝试将我的控制器重定向到('Admin/login'),但它不起作用