Last active
May 31, 2019 20:07
-
-
Save jcunanan05/012350de552495f424dee6ad4dd9c607 to your computer and use it in GitHub Desktop.
difference of async package vs async await. example in https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Displaying_data/Author_detail_page
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
// Display detail page for a specific Author. | |
exports.author_detail = async function(req, res) { | |
// async.parallel( | |
// { | |
// author: function(callback) { | |
// Author.findById(req.params.id).exec(callback); | |
// }, | |
// author_books: function(callback) { | |
// Book.find({ author: req.params.id }, "title summary").exec(callback); | |
// } | |
// }, | |
// function(err, results) { | |
// res.render("author_detail", { | |
// title: "Author Detail", | |
// author: results.author, | |
// author_books: results.author_books | |
// }); | |
// } | |
// ); | |
// try { | |
// const author = await Author.findById(req.params.id); | |
// const author_books = await Book.find( | |
// { author: req.params.id }, | |
// "title summary" | |
// ); | |
// res.render("author_detail", { | |
// title: "Author Detail", | |
// author, | |
// author_books | |
// }); | |
// } catch (error) { | |
// return next(error); | |
// } | |
Promise.all([ | |
await Author.findById(req.params.id), | |
await Book.find({ author: req.params.id }, "title summary") | |
]) | |
.then(([author, author_books]) => { | |
res.render("author_detail", { | |
author, | |
author_books | |
}); | |
}) | |
.catch(err => next(err)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment