我在node.js.中写了一个api。第一个Web服务端点 - /create
- 创建一个带有随机6字符散列的新数据库条目,就像bit.ly散列一样。重构非阻塞nodejs do..while循环
在PHP中做了类似的事情之后,我编写了一个do..while
循环,它会生成一个随机字符串并检查我的mysql数据库(使用node-mysql)以确保它是免费的。我还有一个计数器,所以如果需要的话,我可以在x
迭代后失败。
var i = 0;
var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
var hash = null;
var success = false;
do {
// generate a random hash by shuffling the alphabet,
// joining it and getting 6 chars
hash = alphabet.sort(function(){
return 0.5 - Math.random();
}).join('').substr(0,6);
console.log(i + ': checking hash ' + hash);
// see if it exists in the db
db.query("SELECT hash FROM trips WHERE hash = " + hash, function(err, results){
if(results.length == 0) {
// the hash is free to use :)
success = true;
} else {
// the hash is already taken :(
success = false;
}
});
// increment the counter
i++;
} while(success === false && i < 10);
我目前只在我的分贝(abcdef
)一个哈希值,但环越来越十和失败,因为它认为每一个新的哈希值已经存在。
我敢肯定,这是因为node.js的非阻塞性质。这显然是一件好事,但在我的情况下,我需要循环来阻止,直到查询返回。
我敢肯定,我可以做这样的事情破解这样的:
var q = db.query(...);
但我知道这是扔掉node.js中的一大特色
是否有这种需求的代码模式?
请注意,我做了一个小的变化,以我的答案。 – Tomalak 2012-03-17 21:54:48