Last active
August 29, 2015 14:23
-
-
Save benjamingr/d08cfe94d3f0db1f954d to your computer and use it in GitHub Desktop.
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
function createUser(username, callback) { | |
var connection = DatabaseClient.connect(); | |
var users = connection.call('collection', 'users'); | |
var query = users.call('query', {username: username}); | |
return query.then(function(existing){ | |
if(existing) throw new Error("User already exists: " + username); | |
else return users.call('create', {username: username}); | |
}).fin(function(connection){ return connection.call('close'); }); | |
} |
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
async function createUser(username, callback) { | |
var connection = await DatabaseClient.connect(); | |
try{ | |
var users = await connection.collection('users'); | |
var query = await users.query({username: username}); | |
if(query){ | |
throw new Error("User already exists: " + username); | |
} else { | |
return await users.create({username: username}); | |
} | |
} finally { | |
connection.close(); | |
} | |
} |
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
function createUser(username, callback) { | |
return DatabaseClient.connect().then(function(connection) { | |
return connection.collection('users').then(function(users) { | |
return users.query({username: username}).then(function(existing) { | |
if (existing) { | |
callback("User already exists: " + username); | |
} else { | |
return users.create({username: username}).then(function (user) { | |
callback(null, user); | |
}); | |
} | |
}); | |
}).fin(connection.close); | |
}).catch(callback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@sukima nice, that also works
@abrkn nice, I really should use const more.