典型的JavaScript异步问题

时间:2017-08-28 19:00:27

标签: javascript function asynchronous

我相信这个问题应该相当容易总结,下面的代码会产生以下输出 代码:

var checker = 0;
var increment = 0;
examboardData.forEach(function (examSeed) {
    examBoard.create(examSeed, function (err, exam) {
        console.log("Creating new examboard");
        if (err) {
            console.log("Could not create new examboard\n" + err);
        }
        else {
            console.log("Created examboard");
            increment = 0;
            for (var i = 12 * checker; i < questionData.length - 24 + (12 * checker); i++) {
                console.log("Atleast do this in order");
                question.create(questionData[i], function (err, question) {
                    if (err) {
                        console.log("Could not create new question\n" + err);
                    }
                    else {
                        console.log("Created question");
                        for (var t = math.floor(increment / 4); t < exam.modules.length; t++) {
                            for (var u = 0; u < exam.modules[t].topics.length; u++) {
                                exam.modules[t].topics[u].questions.push(question);
                            }
                        }
                        exam.save();
                    }
                });
                increment++;
            }
            checker++;
        }
    });
});

输出:

Creating new examboard
Created examboard
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Creating new examboard
Created examboard
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Creating new examboard
Created examboard
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Atleast do this in order
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question
Created question

明显的问题是,它只在最后打印Created question而不是按顺序排列,如果有人能提供如何解决这个问题的答案,或者只是指出我正确的方向,我们将不胜感激。 以下是顶部代码(https://hastebin.com/dafugudaya.lua)第204-277行的完整上下文。

0 个答案:

没有答案