I\'m currently trying to learn nodejs and a small project I\'m working is writing an API to control some networked LED lights.
The microprocessor controlling the LED
With ECMA script 2017 (supported by Node 7.6 and above), it becomes a one-liner:
function sleep(millis) {
return new Promise(resolve => setTimeout(resolve, millis));
}
// Usage in async function
async function test() {
await sleep(1000)
console.log("one second has elapsed")
}
// Usage in normal function
function test2() {
sleep(1000).then(() => {
console.log("one second has elapsed")
});
}