javascript - 如何在 React Native 中的前一个函数完成后启动函数
问题描述
我有个问题。我知道我可以使用 Promise 来解决这个问题,但我不明白如何在我的情况下使用它们。我只想id
从另一个文件中的一个函数中获取(我们称之为callApi),然后用它id
来调用另一个函数callApi2。问题是 callApi2 在 apiCall 完成之前运行。
ApiCariIdAktor = {
cariIdAktor: () => {
const apikey = APP_CONSTANT.api_key;
const url = `https://api.themoviedb.org/3/search/person?api_key=${apikey}&language=en-US&query=iko%20uwais&page=1&include_adult=false`;
let id = 0;
fetch(url)
.then((response) => response.json())
.then((responseJson) => {
responseJson.results[0].id
})
.catch((error) => {
console.log(error)
})
return 10;
}
}
setID() {
this.state.id = ApiCariIdAktor.cariIdAktor();
}
async componentDidMount() {
this.setID();
await new Promise(resolve => {
setTimeout(resolve, 1000);
});
this.panggilApi2();
}
解决方案
推荐阅读
- laravel - laravel 客户端 api 与 guzzle
- mysql - 排序规则将 utf8mb4_unicode_ci 更改为 utf8mb4_general_ci
- reactjs - 当上下文消费者中的任何状态发生变化时,React 是否会重新渲染所有组件?
- python - 如何解决“TypeError:不支持的格式字符串传递给 OSError.__format__”?
- python - 用另一个列表中的格式化文本替换列表中的文本
- python - Parser 必须是字符串或字符流,而不是 Series
- mongodb - 我如何将数据插入到 mongodb 的嵌入式集合中
- amazon-web-services - 有人可以为我揭开 AWS 服务中的 EMR/EC2/Lambda/SNS/SQ 的神秘面纱吗?
- ubuntu - --copt -DMESA_EGL_NO_X11_HEADERS --copt -DEGL_NO_X11 是什么意思?
- python - “AttributeError:‘numpy.ndarray’对象没有属性‘值’