我正在使用firebase onauth()方法,但是需要将函数的值返回到调用它的上面的函数。如何将true返回到上面的函数,如下面的代码所示:
signedIn: function(){
ref.onAuth(function(authData) {
if (authData) {
console.log("Authenticated with uid:", authData.uid);
return true
} else {
console.log("Client unauthenticated.")
}
});
}
答案 0 :(得分:4)
使用承诺:
function signedIn(){
var deferred = $q.defer();
ref.onAuth(function(authData) {
if (authData) {
console.log("Authenticated with uid:", authData.uid);
deferred.resolve();
return true
} else {
console.log("Client unauthenticated.")
deferred.reject();
}
});
return deferred.promise;
}
signedIn().then(function () {
alert('Authenfication successful!');
});