-
-
Save JedWatson/8502757 to your computer and use it in GitHub Desktop.
Another refactor of a refactor, this time w/o all original comments but w/ proposals. Not currently possible with asynquence, but maybe? *nudge*
This file contains 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 transform = function(contents, doneTransform) { | |
var authExpiry = 60 * 60 * 1000, | |
authId = uuid.v4(), | |
authKey = 'file:auth:' + authId; | |
redis.client.set(authKey, JSON.stringify({ | |
userId: req.user.id | |
})); | |
redis.client.expire(authKey, authExpiry); | |
// Why no map fn? removes 2 levels of nesting & simplifies the val(fn) | |
ASQ().map(contents.files, function(file, done) { | |
var lookupKey = 'file:path:' + file.path; | |
// I think it's just as clear to use a callback here... actually, simpler. | |
// IMO callback nesting isn't evil, just should be kept in check. | |
redis.client.get(lookupKey, function(err, fileId) { | |
if (err) return done.fail('redis error', err); | |
if (fileId) { | |
fileId = fileId.toString(); | |
} else { | |
fileId = uuid.v4(); | |
redis.client.set('file:id:' + fileId, JSON.stringify(file)); | |
redis.client.set(lookupKey, fileId); | |
} | |
file.path = authId + '/' + fileId; | |
done(file); | |
}); | |
}) | |
.val(function(files) { | |
// benefit of map is that you get a files, much clearer | |
contents.files = files; | |
doneTransform(contents); | |
}) | |
.or(handleError); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment