Issue with Promise chaining

Posted 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:

    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

  • Could it be that you're doing:

    p.then((res)=>{
    func_a(a);
    })
    

    and not:

    p.then((res)=>func_a(a))
    

    The former will return 'undefined', which means the promise will execute immediately, but the latter returns the promise returned by func_a which should allow chaining

  • Thanks Gordon. It has been nearly 2 decades since I used js at all.
    That is at least one step closer. Now it is executing the drawing function sequentially but it is doing it 5 times with the final value of "a".
    It seems it is passing the reference of "a" in stead of the value of "a" to the promise function...
    I forgot the mess javascripts references were in...
    I completely forgot how to pass a variable to a promise without referencing to itself...
    Am I forced to use an array and passing that to the promise function to resolve this or is there a better way to do this?
    So far I have tried quite a few things but it seems all that is passed to the promise function is the final value of "a"

  • :) You can create and execute a function for it...

    let p = func_a(0);
    for (a=20;a<100;a=a+20) 
      (function(a) {
        p=p.then((res)=>func_a(a));
      })(a);
    

    It's a bit crude, but it works

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Issue with Promise chaining

Posted by Avatar for Akisame @Akisame

Actions