gpt4 book ai didi

javascript - 如何在 native react 中调用递归异步函数?

转载 作者:行者123 更新时间:2023-12-02 22:09:04 25 4
gpt4 key购买 nike

我想检查特定的 bool 函数是否返回 true。如果是这样,请运行随机数生成器并尝试再次检查 bool 函数。如此反复,直到结果为假。然后停止递归并结束函数。

这是我想要做的一个想法,但我不太确定如何一次又一次地调用随机数,直到 hasDataBeenSeen() 返回 false。请帮忙!

    async hasDataBeenSeen(key) {
var obj = userData[]; //pre-populated string array

if (obj.indexOf(key) > -1) {
//In the array!
return true
}
else {
//Not in the array
return false
}
}
catch (err) {
console.log(err)
}
}

async executeFunction() {
const dbIndex = this.getUniqueRandomNumber(this.state.ArrayLength); //gets a random +ve integer

//Check if this string in the array has been seen (return true or false)
if(!await this.hasDataBeenSeen(this.state.Data[dbIndex].Key)) ///this.state.Data[] is an array. ALSO if the string is not in the array (i.e. false) then we have what we need and finish the function
{
if(dbIndex != null){
this.setState({ finalString: this.state.Data[dbIndex].Key }); //Set the string locally and finish function
}
}
else
{
//call the random number function again ???
this.getUniqueRandomNumber(this.state.ArrayLength)
}
}

最佳答案

很难理解你的函数如何相互影响,但这可能就是你想要的

waitForSuccess = async () => {
let success = false
let dbIndex
do {
dbIndex = this.getUniqueRandomNumber(this.state.ArrayLength)
success = !(await this.hasDataBeenSeen(this.state.Data[dbIndex].Key))
} while (!success)
return dbIndex
}

// use it somewhere

const dbIndex = await this.waitForSuccess()
this.setState({ finalString: this.state.Data[dbIndex].Key })

关于javascript - 如何在 native react 中调用递归异步函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59618008/

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