Created
May 29, 2013 14:44
-
-
Save vinibaggio/5670821 to your computer and use it in GitHub Desktop.
Diferença visual entre callbacks e promises
This file contains hidden or 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
// Callbacks | |
ObjController.prototype.update = function (req, res) { | |
model.save(function (err, object) { | |
if(err) req.render500(err) | |
otherModel.update(req.form, function (err, object) { | |
if(err) req.render500(err) | |
req.render('obj') | |
}) | |
}) | |
} | |
// Promises | |
ObjController.prototype.update = function (req, res) { | |
model.save() | |
.then(function (object) { | |
return otherModel.update(req.form) | |
}) | |
.then(function () { | |
return req.render('obj') | |
}) | |
.fail(function (err) { | |
return req.render500(err) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment