Created
May 25, 2012 15:18
-
-
Save ForbesLindesay/2788725 to your computer and use it in GitHub Desktop.
Promises (Q)
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
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}); | |
} | |
} | |
}); | |
} |
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
database.findUserByID(1, function(err,user){ | |
if(err) return retry() || throw err; | |
alert("hello " + user.name); | |
}); |
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
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. |
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
$("#myButton").click(function(){ | |
alert("My button was clicked"); | |
}); |
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
/** | |
* 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; | |
} | |
} | |
} | |
} |
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 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 | |
} |
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 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 | |
} |
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
//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; | |
} |
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
function(fn){ | |
return function (){ | |
var cb = arguments.pop(); | |
fn.apply(this, arguments).then(function(res){ | |
cb(null, res); | |
},function(err){ | |
cb(err); | |
}).end(); | |
} | |
} |
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 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