Skip to content

Instantly share code, notes, and snippets.

@joepie91
Forked from dsauerbrun/gist:fdbcabeba7814a7650d8
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joepie91/af4ccaf5b4e6da997952 to your computer and use it in GitHub Desktop.
Save joepie91/af4ccaf5b4e6da997952 to your computer and use it in GitHub Desktop.
var Promise = require("bluebird");
var db = Promise.promisifyAll(db);
function getUser(userId) {
return Promise.try(function(){
return db.findAsync({"selector": {"documentType": "user", "_id": userId}});
}).then(function(userInfo){
if (userInfo.docs.length > 1) {
throw new Error("More than one user returned.");
} else {
return userInfo;
}
})
}
function saveMessage(userId, messageText) {
return Promise.try(function(){
return getUser(userId);
}).then(function(userInfo){
return db.insertAsync({type: 'message', message: messageText, userId: userInfo});
}).then(function(result){
/* Insertion successfully completed. */
}).catch(function(err){
/* Somewhere, an error occurred. You probably shouldn't handle it here, though, but in whatever is calling this function. */
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment