Json 如何将true返回到上面的函数
我正在使用firebase onauth()方法,但需要将函数的值返回到调用它的上面的值。如何将true返回到上述函数,如以下代码所示:Json 如何将true返回到上面的函数,json,angularjs,firebase,Json,Angularjs,Firebase,我正在使用firebase onauth()方法,但需要将函数的值返回到调用它的上面的值。如何将true返回到上述函数,如以下代码所示: signedIn: function(){ ref.onAuth(function(authData) { if (authData) { console.log("Authenticated with uid:", authData.uid); return tru
signedIn: function(){
ref.onAuth(function(authData) {
if (authData) {
console.log("Authenticated with uid:", authData.uid);
return true
} else {
console.log("Client unauthenticated.")
}
});
}
使用承诺:
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!');
});
使用承诺:
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!');
});
如果onAuth是异步的(我假设是),那么您不能。。。您必须将回调传递给signedIn,或使用承诺(请参阅$q)。未立即准备好的返回值通常由
promise
对象实现。如果onAuth是异步的(我假设是),则无法。。。您必须向signedIn传递回调,或者使用承诺(请参见$q)。未立即准备好的返回值通常由promise
对象实现。