简体   繁体   English

等待异步函数返回true或false - 如何检查返回值?

[英]Waiting for async function to return true or false - how do I check return value?

I have an async function songAlreadyInQueue that will return true if an ID is found in a database, false otherwise. 我有一个异步函数songAlreadyInQueue ,如果在数据库中找到ID,则返回true,否则返回false。 I want to use the function like such: 我想使用这样的函数:

if(!songAlreadyInQueue(ref, id)){
  // do stuff
}

But I know that since it's an async function, I have to wait for the result to come back before I truly know if it's true or false. 但我知道,因为它是一个异步函数,所以我必须等待结果回来才能真正知道它是真还是假。 What I have above is always false so I tried this: 我上面的内容总是假的所以我试过这个:

songAlreadyInQueue(ref, id).then((wasFound) => {
                                console.log("wasfound = " + wasFound)
                                if(!wasFound){
                                    //do stuff
                                }
                            })

But that doesn't work either. 但这也不起作用。 What is the proper way to wait for this async function to finish? 等待此异步函数完成的正确方法是什么? This is what my async function looks like (simplified): 这是我的异步函数看起来像(简化):

async function songAlreadyInQueue(requestQueueRef, requestID) {
    var querye = requestQueueRef.where("id", "==", requestID)
        .get()
        .then((snap) => {
            snap.docs.forEach(doc => {
                if (doc.exists) {
                    console.log("**************FOUND REQUEST IN QUEUE ALREADY!")
                    return true
                }
            })
            return false
        })
    return querye // not sure if correct. Is this "returning a promise"?
}

With an async function you should use await instead of then() so you're not creating another anonymous function. 使用异步函数,您应该使用await而不是then()这样您就不会创建另一个匿名函数。 You are returning from an anonymous function rather than the async function. 您将从匿名函数而不是异步函数返回。 try this. 尝试这个。

 async function songAlreadyInQueue(requestQueueRef, requestID) { var snap = await requestQueueRef.where("id", "==", requestID).get() var found = false; snap.docs.forEach(doc => { if (doc.exists) { console.log("**************FOUND REQUEST IN QUEUE ALREADY!") found = true } }) return found } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React JSX 检查异步 function 是否返回 true 或 false - React JSX check if async function return true or false 如何基于HTML函数返回true / false并设置禁用值? - How do I return true/false based on the function to HTML and set the disabled value? 等待不等待异步函数返回值 - await not waiting for async function to return value 返回javascript函数的true / false值,然后根据它做一些东西 - Return true/false value of a javascript function and then do stuff based on that 如何从javascript函数和值返回true? - How do I return true from a javascript function along with a value? 我如何检查数组编号从“pages”到“endingPages”,如果它们匹配返回true,否则为false - how do i check array number form "pages" to "endingPages" if they match return true else is false 我如何在我的jquery中检查php的返回值是“ true”还是“ false”? - How can I check in my jquery if the return value from php is 'true' or 'false'? 如何在同步函数中等待异步返回 - How waiting async return in a sync function 如何在返回true或false之前使函数等待ajax调用返回值? - How can I make a function wait for the ajax call to return a value before returning true or false? 如果给定的小数点四舍五入为整数,如何修改该函数以返回true;否则,返回false? - How do I modify a function to return true if the given decimal is even when it is rounded to an integer and false otherwise?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM