"async https://github.com/caolan/async“是一个非常流行的模块,用于抽象异步循环并使代码更易于阅读/维护。例如:
var async = require('async');
function getHonorStudentsFrom(stuObjList, callback) {
var honorStudents = [];
// The 'async.forEach()' function will call 'iteratorFcn' for each element in
// stuObjList, passing a student object as the first param and a callback
// function as the second param. Run the callback to indicate that you're
// done working with the current student object. Anything you pass to done()
// is interpreted as an error. In that scenario, the iterating will stop and
// the error will be passed to the 'doneIteratingFcn' function defined below.
var iteratorFcn = function(stuObj, done) {
// If the current student object doesn't have the 'honor_student' property
// then move on to the next iteration.
if( !stuObj.honor_student ) {
done();
return; // The return statement ensures that no further code in this
// function is executed after the call to done(). This allows
// us to avoid writing an 'else' block.
}
db.collection("students").findOne({'_id' : stuObj._id}, function(err, honorStudent)
{
if(err) {
done(err);
return;
}
honorStudents.push(honorStudent);
done();
return;
});
};
var doneIteratingFcn = function(err) {
// In your 'callback' implementation, check to see if err is null/undefined
// to know if something went wrong.
callback(err, honorStudents);
};
// iteratorFcn will be called for each element in stuObjList.
async.forEach(stuObjList, iteratorFcn, doneIteratingFcn);
}
所以你可以这样使用它:
getHonorStudentsFrom(studentObjs, function(err, honorStudents) {
if(err) {
// Handle the error
return;
}
// Do something with honroStudents
});
注意.forEach() https://github.com/caolan/async#forEach将为 StuObjList 中的每个元素“并行”调用迭代器函数(即,它不会等待一个迭代器函数完成对一个数组元素的调用,然后再对下一个数组元素调用它)。这意味着您无法真正预测迭代器函数(或更重要的是数据库调用)的运行顺序。最终结果:不可预测的优等生顺序。如果顺序很重要,请使用.forEachSeries() https://github.com/caolan/async#forEachSeries功能。