Hey there,
I'm having some issues with promise chaining and I am wondering if anyone can tell me what am doing wrong.
I have a function that returns a new promise and resolves when an interval function has been cleared.
Something like this:
function func_a(var1, var2, var3, var4) {
return new Promise((resolve, reject)=>{
//math
var pos = 0;
var end_pos = 100;
var id = null;
function draw(){
if (pos == end_pos){
clearInterval(id);
resolve("resolved");
} else {
//drawing function
pos++
}
}
id = setInterval(draw, var4);
});
}
now I have a for loop that is supposed to execute func_a sequentially but no matter how I try with promises it doesn't seem to work.
if (true) {
var p = Promise.resolve();
for (a=0;a<100;a=a+20) {
p=p.then((res)=>{
func_a(a);
});
}
}
for example only draws the thing for the last value of a and
if (true) {
let p = func_a(0);
for (a=20;a<100;a=a+20) {
p=p.then((res)=>{
func_a(a);
});
}
}
draws the first one perfectly followed by the last value of a.
What am I doing wrong?
I checked the references and I couldn't find an async option so I assume that is not a thing for the bangle?
Thanks in advance
ps. I am aware I can do:
let p = func_a(0).then(()=>
func_a(20)).then(()=>func_a(40))
I would just really prefer to do it in a for loop to save a lot of lines of code
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.
Hey there,
I'm having some issues with promise chaining and I am wondering if anyone can tell me what am doing wrong.
I have a function that returns a new promise and resolves when an interval function has been cleared.
Something like this:
now I have a for loop that is supposed to execute func_a sequentially but no matter how I try with promises it doesn't seem to work.
for example only draws the thing for the last value of a and
draws the first one perfectly followed by the last value of a.
What am I doing wrong?
I checked the references and I couldn't find an async option so I assume that is not a thing for the bangle?
Thanks in advance
ps. I am aware I can do:
I would just really prefer to do it in a for loop to save a lot of lines of code