Forked from dsauerbrun/gist:fdbcabeba7814a7650d8
Last active
August 29, 2015 14:26
-
-
Save joepie91/af4ccaf5b4e6da997952 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
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