我知道如何在 Swift 中做到这一点,但对于作为初学者的 Javascript,我正在努力解决这个问题。可以说这是我的功能:
const prom3 = Promise? //this is wrong, I know. Normally in Swift this is an optional
if (statement){
pickerActive = true
prom3 = pathToLottery.once("value", function(values) {
//some function
})
}
if (pickerActive == false){ //normally set to false
return
}else{
return prom3! //not working because this is Swift syntax. How do to this kind of Swift behavior in Javascript? Is it possible?
}
一种可能是在主勺内声明 prom3,但我希望它成为可选的。这可能吗?
这是它应该的类型,也许有帮助:
const prom3: Promise<any> (if I put the function inside the main scoop and hovering my mouse over prom3)
声明 prom3 时不带问号和 !执行时会报错。
最佳答案
您可以采用不需要可选值的方式来构建您的代码,如下所示:
if (statement){
pickerActive = true
const prom3 = pathToLottery.once('value')
prom3.then(snap => {
// some func that has access to data at pathToLottery
const data = snap.val()
return
})
} else {
return
}
查看此视频了解更多信息:https://www.youtube.com/watch?v=NgZIb6Uwpjc&t=35s
关于javascript - 在 Javascript 中是否可以为 Firebase 提供可选的 Promise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43813994/