Skip to content

Instantly share code, notes, and snippets.

@evanshortiss
Last active March 1, 2017 18:54
Show Gist options
  • Select an option

  • Save evanshortiss/93c806f11eeefa9cf0556eee9553677b to your computer and use it in GitHub Desktop.

Select an option

Save evanshortiss/93c806f11eeefa9cf0556eee9553677b to your computer and use it in GitHub Desktop.
// Assume our api does not want these fields returned (ideally we would strip from db query, but you get the idea)
var cleanseData = R.map(R.omit('dob', 'social-security', 'nickname', 'height', 'favourite-movie'));
const age = 25;
// Remove users below an age
var filterData = R.filter((u) => { return u.age >= age });
function getUsersOlderThanAge (age) {
// We could add a catch here if we want to add more error data...
return http.get('http://api.users.com/users.json')
.then(filterData);
}
return Promise.resolve() // starts a chain, any subsequent code if it "throws" will be caught safely
.then(() => getUsersOlderThanAge(age))
.then((users) => cleanseData(users))
.catch((e) => {
log.error(new VError(e, 'failed to get users'));
});
// And now with callbacks...................
function getUsersOlderThanAge (age, callback) {
return http.get('http://api.users.com/users.json', function (err, res) {
if (err) {
callback(err);
} else {
callback(null, filterData(res));
}
})
}
async.waterfall([
function (next) {
getUsersOlderThanAge(age, next)
},
function (users, next) {
next(null, cleanseData(users))
}
], (e, res) => {
if (e) {
log.error(new VError(e, 'failed to get users'));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment