admin管理员组文章数量:1195282
Several times now I have run into an issue with synchronous & asynchronous functions using Firebase. My problem is often that I need to make an asynchronous Firebase call within a function that I wrote. As a simple example, suppose I need to calculate & display the velocity of an object, and my Firebase stores distance & time:
function calcVelocity() {
var distance, time, velocity;
firebaseRef.once('value', function(snapshot) {
distance = snapshot.val().distance;
time = snapshot.val().time;
velocity = distance / time;
});
return velocity;
}
$("#velocity").html(calcVelocity());
Of course, the above code will not work because firebaseRef.once()
is an asynchronous call, so velocity
has not been set yet when we reach return velocity;
. If we place the return
inside the .on()
callback function, then nothing is returned at all.
One solution would be to make my calcVelocity()
function asynchronous as well.
Another solution would be to store a cached version of the Firebase that is read synchronously but updated asynchronously from the Firebase.
Is one of these solutions better than the other? And is there a better solution?
Several times now I have run into an issue with synchronous & asynchronous functions using Firebase. My problem is often that I need to make an asynchronous Firebase call within a function that I wrote. As a simple example, suppose I need to calculate & display the velocity of an object, and my Firebase stores distance & time:
function calcVelocity() {
var distance, time, velocity;
firebaseRef.once('value', function(snapshot) {
distance = snapshot.val().distance;
time = snapshot.val().time;
velocity = distance / time;
});
return velocity;
}
$("#velocity").html(calcVelocity());
Of course, the above code will not work because firebaseRef.once()
is an asynchronous call, so velocity
has not been set yet when we reach return velocity;
. If we place the return
inside the .on()
callback function, then nothing is returned at all.
One solution would be to make my calcVelocity()
function asynchronous as well.
Another solution would be to store a cached version of the Firebase that is read synchronously but updated asynchronously from the Firebase.
Is one of these solutions better than the other? And is there a better solution?
Share Improve this question asked Jul 24, 2012 at 18:06 Matt RobertsonMatt Robertson 3,1655 gold badges38 silver badges64 bronze badges 1 |3 Answers
Reset to default 9Another approach is to utilize a Promise strategy. jQuery has a great one.
function calcVelocity() {
var distance, time, velocity, def = $.Deferred();
firebaseRef.once('value', function(snapshot) {
distance = snapshot.val().distance;
time = snapshot.val().time;
def.resolve( distance / time );
});
return def.promise();
}
calcVelocity().then(function(vel) { $("#velocity").html(vel); });
Keep in mind also that snapshot.val().distance;
may return an error if snapshot.val()
returns null!
You nailed the two possibilities: Either make your function asynchronous as well, or cache the latest Firebase data so you can access it synchronously. Which one you use is just a matter of preference and convenience, given the context of the app you're writing.
For instance, we've noticed that "action games" are usually driven by a tight render loop instead of by firebase data change events. So it makes sense to cache the latest Firebase data for use in your render loop. For example:
var latestSnapshot = null;
firebaseRef.on('value', function(snap) { latestSnapshot = snap; });
And then you can use latestSnapshot synchronously in your render loop (or wherever else), though you need to be careful to deal with it being null until the first firebase callback happens.
Same idea as in the answer @Kato provided, but with the built-in promises in Firebase would look something like this
function calcVelocity(snapshot) {
var distance, time, velocity;
distance = snapshot.val().distance;
time = snapshot.val().time;
return distance / time;
}
function getVelocity() {
return firebaseRef.once('value').then(calcVelocity);
}
getVelocity().then(function(vel) { $("#velocity").html(vel); });
本文标签: javascriptHandling Asynchronous Calls (Firebase) in functionsStack Overflow
版权声明:本文标题:javascript - Handling Asynchronous Calls (Firebase) in functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738467978a2088413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
calcVelocity
accept a callback or return a deferred object. – Felix Kling Commented Jul 24, 2012 at 18:09