admin管理员组文章数量:1327713
I have an async function songAlreadyInQueue
that will return true if an ID is found in a database, false otherwise. 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 e 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"?
}
I have an async function songAlreadyInQueue
that will return true if an ID is found in a database, false otherwise. 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 e 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"?
}
Share
Improve this question
asked Jun 13, 2019 at 2:04
Isaac PerezIsaac Perez
5904 gold badges17 silver badges33 bronze badges
5
- 2 The way you tried that "doesn't work" is basically the right way to do it. What exactly doesn't work about that way? – Joseph Sible-Reinstate Monica Commented Jun 13, 2019 at 2:09
- "Is this "returning a promise"?" yes – Phil Commented Jun 13, 2019 at 2:12
- @JosephSible wasFound is always false – Isaac Perez Commented Jun 13, 2019 at 2:13
-
That's because the return value of the
forEach
callback is not considered – Phil Commented Jun 13, 2019 at 2:16 - @Phil ooooh so I was returning true for the forEach callback instead of returning true for songAlreadyInQueue? – Isaac Perez Commented Jun 13, 2019 at 2:21
1 Answer
Reset to default 6With an async function you should use await instead of then()
so you're not creating another anonymous function. 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
}
版权声明:本文标题:javascript - Waiting for async function to return true or false - how do I check return value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742213982a2434229.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论