I am working with Node.js module AR-Drone to control my parrot ar. drone 2.0. and I am using this structure.
client.takeoff();
client.after(5000, function () {
this.on("navdata", function (d) {
if (d.demo) {
//totaly and totalx are calculated here using navdata
if (totaly > 85) {
// if the deviation is far to the right more than 85 mm
console.log("moving left ");
client.left(0.15);
sleep(500).then(() => {
client.stop();
});
} else if (totaly < -85) {
// if the deviation is far to the left more than 85 mm
console.log(" moving right");
client.right(0.15);
sleep(500).then(() => {
client.stop();
});
} else if (totalx < 3000) {
client.front(0.02);
sleep(250).then(() => {
client.stop();
});
console.log("moving forward");
} else {
client.land();
}
}
});
});
My question is that when I write sleep(500) and then do something, does the program actualy wait 500 ms or the "if cycles" run at the same time? what is the order of the actions sent to the drone?
The
sleepfunction does not block the thread for the duration but instead schedules the callback to run right after the time has passed. However, the code outside of the callback runs synchronously.So, if you want a guarantee that something runs "after the sleep", you have to put it inside the
.then()callback or useasync/await.Example with
async/await: