-
-
Save joshhunt/e3761594d10bb7025bcb 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) { | |
DatabaseClient.connect() | |
.then(function(connection) { | |
return connection.collection('users') | |
}) | |
.then(function(users) { | |
return users.query({username: username}) | |
}) | |
.then(function(exising){ | |
if(existing) { | |
throw new Error('User already exists'); | |
} | |
return users.create({username: username}) | |
}) | |
.then(function(newUser) { | |
callback(null, newUser); | |
}) | |
.catch(callback) | |
} |
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