Skip to content

Instantly share code, notes, and snippets.

@ForbesLindesay
Created May 25, 2012 15:18
Show Gist options
  • Save ForbesLindesay/2788725 to your computer and use it in GitHub Desktop.
Save ForbesLindesay/2788725 to your computer and use it in GitHub Desktop.
Promises (Q)
function getAllUserDetails(id, cb){
database.findUserByID(id, function(err,user){
if(err) return cb(err);
var roles,friends;
user.getRoles(function(err,res){
if(err) return cb(err);
roles = res;
next();
});
user.getFriends(function(err,res){
if(err) return cb(err);
friends = res;
next();
});
function next(){
if(roles&&friends){
cb(null, {user:user, roles:roles, friends:friends});
}
}
});
}
database.findUserByID(1, function(err,user){
if(err) return retry() || throw err;
alert("hello " + user.name);
});
getAllUserDetails(1).then(function(details){
//this is where you can use the return value
//of the function (details)
},function(err){
//if there is an error, it can be caught in
//this optional function
//If you want to re-throw the error you should
//Use:
return Q.reject(err);
}).end();//always call end unless you're returning a promise.
$("#myButton").click(function(){
alert("My button was clicked");
});
/**
* Wrap a function so that it may be called by someone who uses
* promises, or someone who prefers to stick to plain callbacks
*
* You can even write your function either way round too. The
* only requirement is that you can't take a function as the
* last argument to your function, other than a callback.
*
* @param {Function} fn The function to wrap
* @param {Boolean} promised Is the function being wrapped promised
* @return {Function} A wrapped function
*/
function(fn,promised){
return function(){
var args = Array.prototype.slice.call(arguments);
if(promised){
if(typeof args[args.length-1] === 'function'){
var cb = args.pop();
fn.apply(this, args).then(function(res){
cb(null,res);
},function(err){
cb(err);
}).end();
} else {
return fn.apply(this, args);
}
} else {
if(typeof args[args.length-1] === 'function'){
fn.apply(this, args);
} else {
var res = Q.defer();
args.push(res.makeNodeResolver());
fn.apply(this, args);
return res.promise;
}
}
}
}
var getAllUserDetails = function (id){
return database.findUserByID(id)
.then(function(user){
var rolesP = user.getRoles();
var friendsP = user.getFriends();
return rolesP.then(function(roles){
return friendsP.then(function(friends){
return {user:user, roles:roles, friends:friends};
});//friendsP
});//rolesP
});//userP
}
var getAllUserDetails = function (id){
return database.findUserByID(id)
.then(function(user){
return Q.all(user.getRoles(), user.getFriends()).spread(function(roles,friends){
return {user:user, roles:roles, friends:friends};
});//rolesP and friendsP
});//userP
}
//promised to cb
function getAllUserDetailsCB(id, cb){
getAllUserDetails(id).then(function(details){
cb(null, details);
},function(err){
cb(err);
}).end();
}
//cb to promised
function getAllUserDetails(id){
var res = Q.defer();
getAllUserDetailsCB(id,function(err,data){
if(err)res.reject(err);
else res.resolve(data);
});
return res.promise;
}
function(fn){
return function (){
var cb = arguments.pop();
fn.apply(this, arguments).then(function(res){
cb(null, res);
},function(err){
cb(err);
}).end();
}
}
var getAllUserDetails = Q.async(function (id){
var user = yield database.findUserByID(id);
var rolesP = user.getRoles();
var friendsP = user.getFriends();
var roles = yield rolesP;
var friends = yield friendsP;
Q.return({user:user, roles:roles, friends:friends});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment