node、redis、asyncを取得して自分のやりたいことを実行するのに苦労しています。制御フローのリダイレクトのパターンを把握するために、非常に基本的なことを試みています。key0 > key1
ここでは、比較が真の場合に1ずつ増加するカウンター変数「success」を保持しています。それらは今のところ静的なので、常に真実です。変更したいのはインクリメントすることだけですsuccess
。ブラウザを更新して比較を再実行し、success
再度インクリメントします。
私の問題は、ページが更新されるとsuccess
2ジャンプすることです。を使用してコールバックを設定しようとしましたが、 -typeコマンドincr
のみがコールバックを持っているようです。get
スクリプトにclient.end();
がありますが、ページを再読み込みできなかったため、コメントアウトしました。これが私の問題の原因だと思います。もしそうなら、どこにclient.end
属しますか?
var http = require("http");
var redis = require("redis");
var async = require("async");
client = redis.createClient();
http.createServer(function(request, response) {
// key "success" set to 0 externally, through redis-cli;
client.set("key0", "19");
client.set("key1", "11");
response.writeHead(200, {"Content-Type": "text/plain"});
async.series([
shortcut("key0"),
shortcut("key1"),
shortcut("success")
],
function(err, results){
if (results[0] > results[1]) {
client.incr("success", function(err, reply) {
response.write("incr done");
});
response.write(results[0] + "\n\n");
response.write(results[1] + "\n\n");
response.write(results[2]);
}
response.end();
// client.quit();
});
}).listen(8000);
function shortcut(key) {
return function(callback) {
client.get(key, function(err, reply) {
callback(null, reply);
}
);
}
}