Skip to content

Instantly share code, notes, and snippets.

@marktyers
Created August 28, 2015 10:05
Show Gist options
  • Save marktyers/505cfad671d07c5c58cf to your computer and use it in GitHub Desktop.
Save marktyers/505cfad671d07c5c58cf to your computer and use it in GitHub Desktop.
// calling multiple promises
exports.addNewProfile = function(token, auth, body, callback) {
var beacon
checkNewToken(token).then(function() {
return checkAuthPresent(auth)
}).then(function() {
console.log('UN: '+auth.basic.username)
return checkUniqueEmail(auth.basic.username)
}).then( function() {
return checkDataPresent(body)
}).then(function() {
return getNewId()
}).then(function(data) {
beacon = data.message
return addPhoneNode(token, beacon.major, beacon.minor)
}).then(function(data) {
return encryptPassword(auth.basic.username, auth.basic.password)
}).then(function(data) {
return buildPhoneDocument(token, beacon, data.message, body)
}).then(function(data) {
return insertNewDocument(data);
}).then(function(data) {
callback(data)
}).catch(function(err) {
callback(err);
})
}
// a sample function returning a promise.
function getProfileData(auth) {
console.log('3. GET PROFILE DATA')
return new Promise(function(resolve, reject) {
var email = auth.basic.username
console.log('EMAIL: '+email)
// search based on the username
// db.nodes.find({'auth.email':'[email protected]'})
// return the document
mongo.Node.find({'auth.email': email}, {'_id': 0, '__v': 0}, function(error, document) {
try {
if (error) {
return reject({code: 404, status:'error', message:error})
}
var profile = data.document[0]
return resolve({code: 201, status:'success', document: profile})
} catch(err) {
return reject({code: 404, status:'error', message:'email address has not been registered'})
}
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment