Created
January 6, 2012 14:37
-
-
Save insin/1570856 to your computer and use it in GitHub Desktop.
Before and after caolan/async (not tested yet!)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $r = redis client | |
// $f = util.format | |
function createUser(username, email, password, cb) { | |
$r.incr('users.count', function(err, id) { | |
if (err) return cb(err) | |
$r.set($f('username.to.id:%s', username.toLowerCase()), id, function(err) { | |
if (err) return cb(err) | |
var user = {id: id, username: username, email: email} | |
getRandom(function(err, salt) { | |
if (err) return cb(err) | |
hashPassword(password, salt, function(err, hp) { | |
if (err) return cb(err) | |
user.salt = salt | |
user.password = hp | |
$r.hmset($f('user:%s', id), user, function(err) { | |
if (err) return cb(err) | |
cb(null, user) | |
}) | |
}) | |
}) | |
}) | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $r = redis client | |
// $f = util.format | |
function createUser(username, email, password, cb) { | |
var user = {username: username, email: email} | |
async.auto({ | |
id: $r.incr.bind($r, 'users.count') | |
, salt: getRandom | |
, hashedPassword: ['salt', function(callback, results) { | |
hashPassword(password, results.salt, callback) | |
}] | |
, storeId: ['id', 'hashedPassword', function(callback, results) { | |
$r.set($f('username.to.id:%s', username.toLowerCase()), results.id, callback) | |
}] | |
, storeUser: ['id', 'hashedPassword', function(callback, results) { | |
user.id = results.id | |
user.salt = results.salt | |
user.password = results.hashedPassword | |
$r.hmset($f('user:%s', user.id), user, callback) | |
}] | |
} | |
, function(err) { | |
if (err) return cb(err) | |
cb(null, user) | |
} | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = require('async') | |
var delay = 1000 | |
, ms | |
function test() { | |
async.auto({ | |
id: function(callback) { | |
ms = +new Date | |
console.log('>>> 1.1') | |
setTimeout(function() { console.log('<<< 1.1'); callback(null, 42) }, delay) | |
} | |
, salt: function(callback) { | |
var m = +new Date | |
console.log('%sms since last call', m - ms) | |
ms = m | |
console.log('>>> 1.2') | |
setTimeout(function() { console.log('<<< 1.2'); callback(null, 'saxo') }, delay) | |
} | |
, hashedPassword: ['salt', function(callback, results) { | |
var m = +new Date | |
console.log('%sms since last call', m - ms) | |
ms = m | |
console.log('>>> 2') | |
console.log('hashing with salt = %s', results.salt) | |
setTimeout(function() { console.log('<<< 2'); callback(null, 'hash') }, delay) | |
}] | |
, storeId: ['id', 'hashedPassword', function(callback, results) { | |
var m = +new Date | |
console.log('%sms since last call', m - ms) | |
ms = m | |
console.log('>>> 3.1') | |
console.log('storing id = %s', results.id) | |
setTimeout(function() { console.log('<<< 3.1'); callback(null, true) }, delay) | |
}] | |
, storeUser: ['id', 'hashedPassword', function(callback, results) { | |
var m = +new Date | |
console.log('%sms since last call', m - ms) | |
ms = m | |
console.log('>>> 3.2') | |
console.log('storing user with = %j', results) | |
setTimeout(function() { console.log('<<< 3.2'); callback(null, true) }, delay) | |
}] | |
} | |
, function(err, results) { | |
console.log('%sms since last call', +new Date - ms) | |
console.log('final callback err = %j', err) | |
console.log('final callback results = %j', results) | |
} | |
) | |
} | |
test() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 1.1 | |
54ms since last call | |
>>> 1.2 | |
<<< 1.1 | |
<<< 1.2 | |
1016ms since last call | |
>>> 2 | |
hashing with salt = saxo | |
<<< 2 | |
1141ms since last call | |
>>> 3.2 | |
storing user with = {"id":42,"salt":"saxo","hashedPassword":"hash"} | |
<<< 3.2 | |
1018ms since last call | |
>>> 3.1 | |
storing id = 42 | |
<<< 3.1 | |
1007ms since last call | |
final callback err = null | |
final callback results = {"id":42,"salt":"saxo","hashedPassword":"hash","storeUser":true,"storeId":true} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 1.1 | |
7ms since last call | |
>>> 1.2 | |
<<< 1.1 | |
<<< 1.2 | |
1014ms since last call | |
>>> 2 | |
hashing with salt = saxo | |
<<< 2 | |
1014ms since last call | |
>>> 3.2 | |
storing user with = {"id":42,"salt":"saxo","hashedPassword":"hash"} | |
2ms since last call | |
>>> 3.1 | |
storing id = 42 | |
<<< 3.2 | |
<<< 3.1 | |
1013ms since last call | |
final callback err = null | |
final callback results = {"id":42,"salt":"saxo","hashedPassword":"hash","storeUser":true,"storeId":true} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment