JS等待一个函数在第二个之前完成,首先是异步吗?

时间:2016-12-31 06:25:15

标签: javascript

var membersStatArray = [];

function runFirst (callback) {
    db.group.findOne({_id: id}).then((groupFound) => {
        var membersArray = groupFound.members;
        membersArray.forEach(function (member) {
            db.doneTodo.find({'victor._id': member._id}).then((arrayOfUserVictories) =>{
                membersStatArray.push({email: member.email, victories: arrayOfUserVictories.length});
            })
        });
        callback();
    });

}

function runSecond() {
    console.log(membersStatArray);
    res.status(305).send();
}

runFirst(runSecond);

runFirst中的函数是异步的,所以我打电话给控制台打印出secondFunc中的membersStatArray,但它仍然打印[],我怎么能用这个回调功能?

0 个答案:

没有答案