I have this code originally in python.
SendSerialPortCommand("XXX")
time.delay(0.5)
SendSerialPortCommand("YYY")
I converted this code to node.js but the code looks much uglier.
SendSerialPortCommand("XXX");
setTimeout(function () {
SendSerialPortCommand("YYY");
}, 500);
Imagine if my python code looks like this.
SendSerialPortCommand("XXX")
time.delay(0.5)
SendSerialPortCommand("YYY")
time.delay(0.5)
SendSerialPortCommand("AAA")
time.delay(0.5)
SendSerialPortCommand("BBB")
The node.js code will look really ugly with setTimeout()
inside setTimeout()
.
How can the node.js code be improved in terms of readability? I don't care about violating asynchronous nature of javascript for this question. Important thing is readability.
Previously accepted solution just complicates the things, and not brings any readability or improvement. Do it like this then, just one-liners:
setTimeout(function(){ SendSerialPortCommand("XXX"); }, 500);
setTimeout(function(){ SendSerialPortCommand("YYY"); }, 1500);
setTimeout(function(){ SendSerialPortCommand("ZZZ"); }, 2000);
If you want to make it configurable, move options to the config above, and call in the loop, alike:
var schedulerData = [
{delay: 500, params: "XXX"},
{delay: 1500, params: "YYY"},
{delay: 2000, params: "ZZZ"}
];
for (var i in schedulerData) {
var doTimeout = function(param, delay) {
setTimeout(function(){ SendSerialPortCommand(param); }, delay );
};
doTimeout(schedulerData[i].params, schedulerData[i].delay);
}
Here's the JSFiddle, to play with.
node-fibers
If you want advanced solution through node.js to "show off", you may go node-fibers
way, and to create sleep function, alike in their manual.
var Fiber = require('fibers');
function sleep(ms) {
var fiber = Fiber.current;
setTimeout(function() {
fiber.run();
}, ms);
Fiber.yield();
}
Fiber(function() {
SendSerialPortCommand("XXX");
sleep(1000);
SendSerialPortCommand("YYY");
}).run();
console.log('still executing the main thread');
node-fibers
implemenation is being used in tons of other smaller libraries, alike WaitFor. More information could be found here.
Promise
& Deferred
ObjectsYou can create a Promise based timeout function. Joe described one of possible implementations. But I will provide small code snippet, for easier understanding on how it actually works, using Defferred
from jQuery:
function wait(ms) {
var deferred = $.Deferred();
setTimeout(deferred.resolve, ms);
// We just need to return the promise not the whole deferred.
return deferred.promise();
}
// Use it
wait(500).then(function () {
SendSerialPortCommand("XXX");
}).wait(500).then(function () {
SendSerialPortCommand("YYY");
});
If promises are not supported, you will need to get polyfills for ECMAScript, for example Promises from core-js
package or any other standalone component of Promises/A+ implementation.
Deffered, might be gotten as separate Deffered
package for NPM as well, the concept is nicely described here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With