【发布时间】:2016-10-23 20:44:25
【问题描述】:
我想顺序执行四个代码块,并在这个顺序执行期间进行测试。挑战在于这些块包含异步函数的调用。
我似乎无法完全接受正确使用它们的承诺,在代码块上花费了几个小时后,我似乎陷入了困境。
// Store current log of drone visits for DJI Phantom 4
db.query("SELECT COUNT(drone_id) FROM Drone_Visits WHERE drone_id = 2;", function(err, rows) {
if (err) {
console.log(err);
}
else {
current_drone_visits = rows[0]['COUNT(drone_id)'];
}
});
it("should return status 200 (OK) when requesting route for this function", function(done) {
request.get("/product/DJI/Phantom_4").query({brand: dji_brand, model: dji_model}).end(function(err, res) {
assert.equal(res.status, 200);
done();
});
});
db.query("SELECT COUNT(drone_id) FROM Drone_Visits WHERE drone_id = 2;", function(err, rows) {
if (err) {
console.log(err);
}
else {
updated_drone_visits = rows[0]['COUNT(drone_id)'];
}
});
it("should increment the value of drones visited in the database", function(done) {
console.log("A - " + current_drone_visits);
console.log("B - " + updated_drone_visits);
assert.equal(current_drone_visits + 1, updated_drone_visits);
done();
});
如果我想链接我的回调以使其仅在前一个函数完成后执行,我应该在此处做什么?
【问题讨论】:
-
为了确保我正确理解您的问题:您想运行一个查询,然后进行测试,然后运行另一个查询,然后再进行另一个测试?
标签: javascript testing asynchronous callback promise