What you're doing there seems really nice and clean.
The downside is that when it first starts, it's using a bit of memory to store all the promises all at once, but it's not really that bad.
If you nested everything with callbacks you'd avoid that up-front memory usage, but then it ends up re-parsing all the functions inside each time it goes through an iteration, so there are downsides there too (in addition to it looking awful).
Only other thing I might mention is if you're just doing:
spiWriteCommand(X);
return hspi_cmd(Y);
over and over, you could create an array, like:
var data = [
[[sizeH-1,0,0],0x0C],
[[0xD7,0xD6,0x9D],0x2C],
[0xA8,0x3A],
...
];
promise = new Promise(resolve => {
function out() {
var cmd = data.shift();
if (!cmd) return resolve();
spiWriteCommand(cmd[0]);
hspi_cmd(cmd[1]).then(out);
}
out();
});
but I wonder if that's really better - i'd stick with what you have for now
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
What you're doing there seems really nice and clean.
The downside is that when it first starts, it's using a bit of memory to store all the promises all at once, but it's not really that bad.
If you nested everything with callbacks you'd avoid that up-front memory usage, but then it ends up re-parsing all the functions inside each time it goes through an iteration, so there are downsides there too (in addition to it looking awful).
Only other thing I might mention is if you're just doing:
over and over, you could create an array, like:
but I wonder if that's really better - i'd stick with what you have for now