I'm new to mongoose and I'm trying to understand what exactly is happening when 'callback' is being passed. I understand that async.parallel is calling two functions that return the results of the query, but I don't understand the use of 'callback' in this situation.
async.parallel({
author: function(callback) {
Author.findById(req.body.authorid).exec(callback)
},
authors_books: function(callback) {
Book.find({ 'author': req.body.authorid }).exec(callback)
},
}, function(err, results){
// some function
}
calling callback
means you are done with that specific operation.
async.parallel
waits until calling all of the callbacks, then call the last function and passes the results to it which obtains from callbacks in every single operation before.
So in your case, async
calls both of your operations at the same time. When mongoose find or throw an error by finding the author by req.body.authorid
calls the callback, same for authors_books
. For the purpose of determining when all of the parallel functions are done, the last function has been called with err
and results
parameters. you can check the err
and results
to find out how were done the operations.
Note : You will get the result of the operation in results parameter in the last function. for example, you will get author
from the first operation in results.author
and also results.authors_books
come from the second operation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.