I'm getting rid of my blocking infinite while loop and substituting it with promises. I have a simple run function which lights up an LED and turns it off after which it continues to the next one.
Obviously Promises don't work inside while loops so I was wondering how I can use recursion to go back into my run()
function and also increment my counter x
?
var x = 0
var sign = 1
function run() {
return new Promise((resolve, reject) => {
try {
wait(20).then(() => { device.setColor("random", { 'channel': 0, 'index': x }) })
wait(20).then(() => { device.setColor("#000000", { 'channel': 0, 'index': x }) })
x += sign
if (x == LED_COUNT - 1) {
sign = -1
} else if (x == 0) {
sign = 1
}
return resolve()
} catch (e) {
return reject(e)
}
})
}
run() // this only runs once, I need it to run forever