Jawaban singkatnya adalah Anda tidak berpikir secara tidak sinkron. Karena Anda menggunakan fungsi asinkron dalam fungsi Anda, fungsi Anda juga harus asinkron.
Karena Anda tidak memposting sisa kode Anda, inilah ide dasarnya:
var client = require('redis').createClient();
function createMobs(callback) {
var mobObject = { name: 'Goblin' };
client.hmset('monsterlist', 'mobs', JSON.stringify(mobObject), function(err) {
// Now that we're in here, assuming no error, the set has went through.
client.hgetall('monsterlist', function(err, object) {
// We've got our object!
callback(object);
});
// There is no way to run code right here and have it access the object variable, as it would run right away, and redis hasn't had time to send you the data yet. Your myMobs function wouldn't work either, because it is returning a totally different function.
});
};
app.get('/create', function(req, res) {
createMobs(function(object) {
res.render('mobs.jade', {
mobs: object
});
});
});
Semoga itu membantu memperjelas semuanya.