[英]Waiting for loading data async
I have some problem with js.我对js有一些问题。 So I fetch some data from API and I need to start some function, which can show the page.所以我从 API 取一些数据,我需要启动一些 function,它可以显示页面。 So my code所以我的代码
route.js路由.js
export let rate = {};
const getNewRate = async () => {
const responce = await fetch('https://www.cbr-xml-daily.ru/daily_json.js');
const json = await responce.json();
rate = json.Valute;
await setRateExc(rate);
await setRate(rate);
};
getNewRate();
export const onClickExchange = () => {
root.innerHTML = null;
exchange();
};
export const onClickCurExchange = () => {
root.innerHTML = null;
getRate();
};
So, this is the code of router, I need that getNewRate finish before starting onClickExchange in other file.所以,这是路由器的代码,我需要在其他文件中启动 onClickExchange 之前完成 getNewRate。 How I can make it?我怎样才能做到?
This is my try.这是我的尝试。
setTimeout(() => {
onClickExchange();
}, 500)
A sample to go on with一个样本到 go on with
const rate = "";
async fetchUsersDataRequest = () => {
const url = `the-api-url-here`;
//...
await fetch(url)
.then((res) => res.json())
.then((res) => {
console.log(res.Value); // Valute or Value ?
rate = res.Value;
})
.catch((error) => {
console.log(error);
//...
});
await setRateExc(rate);
await setRate(rate)
};
Okay, got it.好,知道了。 I can use onClickExchange in getNetRate().then(() => onClickExchange())我可以在 getNetRate().then(() => onClickExchange()) 中使用 onClickExchange
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.