gpt4 book ai didi

How can I make function that waits for an asnyc function to finish before running?(如何使函数在运行前等待asnyc函数完成?)

转载 作者:bug小助手 更新时间:2023-10-28 10:19:08 24 4
gpt4 key购买 nike



Currently I have a async function that is using a fetch request to receive data once that data is received I want it to run checkVals right away. Only problem is that checkVals is running before my async function is finished. How can I have checkVals wait for the async function to finish before running?

目前,我有一个使用FETCH请求接收数据的异步函数,一旦接收到数据,我希望它立即运行CheckVals。唯一的问题是,在我的异步函数完成之前,check Vals正在运行。如何让CheckVals在运行之前等待异步函数完成?


Original Async

原始异步


async function getData(quData) {
let res = await fetch("http://localhost:9000/testAPI/api/upload", {
method: "GET",
headers: { "content-type": "application/JSON" },
});
data = await res.json();
return (quData = {
Title: data.Title,
Question: data.Question,
DoC: data.DoC,
});
}

Original Async without fetch

不带回迁的原始异步


const data = null
const quData = null

async function getData() {
data = {
Title: "Title",
Question: "Question",
DoC: "DoC"
}
return (quData = {
Title: data.Title,
Question: data.Question,
DoC: data.DoC,
});
}

Function to run after Async

要在异步后运行的函数


const checkVals = () =>{
console.log(quData)
}

The only thing I've tried checking for is if quData is a truthy value and it wasn't working.

我尝试检查的唯一一件事是,quData是否是一个真实值,但它不起作用。


更多回答

await the function?

等待功能吗?

getData().then(() => checkVals()); or await getDate(); checkVals()

GetData().Then(()=>CheckVals());或等待getDate();CheckVals()

@Yousaf that worked. Thanks, I'm kinda stupid.

@Yousaf奏效了。谢谢,我有点傻。

优秀答案推荐
更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com