【发布时间】:2018-01-19 12:30:25
【问题描述】:
所以我研究了 Promises 并得出结论,通过使用.then,它将“实际执行”其中的异步函数。然后,我继续更新我的代码:
function getCurrentKey(){
return tailorRef.once('value').then(function(data){
data.forEach(function(childData){
if ( (loggedUname == childData.val().tUsername) && (loggedPword == childData.val().tPassword) ){
Ukey = childData.key;
}
});
return Ukey;
});
}
currentKey = getCurrentKey();
console.log("key = " + currentKey);
注意:var currentKey 是全局的
控制台中的预期输出将是key = "some unique key",但它显示key = [object Promise]。我试图将.val() 放在childData.key 之后以实际获取值而不是对象,但它不起作用。
我的 Promise 实现或其结构有问题吗?还是我的结论错了?
【问题讨论】:
-
getCurrentKey中有两个 return 语句 - 第二个被忽略 - 因此,该函数返回的是由return tailorRef.once('value').then(function(data){返回的 Promise -
@guest271314 - 不是真的。向不懂异步代码的人解释异步代码是相当困难的
-
@JaromandaX 啊,我的错。对不起,我没有早点意识到。
-
还有更多问题,因为你需要知道如何使用 Promises
标签: javascript firebase firebase-realtime-database promise