I've been exploring javascript more deeply lately, playing around with a node,redis,socket.io,express simple webapp.
I think I've come to a problem that is an example of the fundamental difference between JS and PHP, which is what I've been using the last few years.
I've written this callback and need to return a stack of data from redis
app.get('/deck', function(request, response) {
dealer.sort('cards', 'BY', 'deal:*->timestamp', 'GET', 'card:*->card_key',
function(err, card_keys) {
var deck = [];
for (k in card_keys) {
dealer.hget(card_keys[k], 'data', function(err, card_data) {
var deal = eval('(' + card_data +')');
deals.push(cards);
});
}
response.json(deals);
}
);
});
Now first I thought it was a variable scoping problem so I rewrote with a closure (did I use this term correctly?) which didn't work because I am writing this in synchronous mind set. I realize that this is essentially synchronous and sends the data out before it is collected. The design is wrong.
I can rewrite the function to use socket.io's emit function to send data as it is collected.
But are there design patterns for handling synchronous data? What if I want to show the last 10 cards in order? Do I send the data over anyway and have client side code queue up 10, and trigger an event that sorts and then control display?
Edit: I found this question which essentially mine, except that I would like to figure out how to design this without relying an synchronous library.