Skip to content

Instantly share code, notes, and snippets.

@JedWatson
Forked from getify/async.js
Last active January 3, 2016 18:29
Show Gist options
  • Save JedWatson/8502757 to your computer and use it in GitHub Desktop.
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*
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