Async handling

Posted on
  • Promises are a powerful tool to avoid callback-hell.
    For Espruino I was searching for a tiny solution and found 7 Lines from Krasimir.
    Based on this I wrote a simple flow solution.

    var async = function(funcs,scope,done ) {
      var _seq = 0,stopped = false;
      this.scope = scope; = function(){this.goto(_seq,arguments);};
      this.goto = function(seq,args){
        if(seq < funcs.length){
          if(!stopped){ funcs[seq](args);_seq = seq + 1;} }
      this.exit = function(){stopped = true;done(arguments);};;

    It supports

    1. step by step using next()
    2. jumping from function to function using goto(funcNr)
    3. option to exit in each function using exit()
    4. local data using scope-object
    5. data handover from function to function, see async2, async3

    Handling is simple as you can see here:

    function logger(seq,value){ console.log(seq,new Date(),value); }
    function async1(){
        { title:"async1",value: 1},
        function(){logger("1 done",scope);async2();}
    function async2(){
            scope.n_val = 22;
        { title:"async2",value: 2},
        function(){logger("2 done",scope);async3();}
    function async3(){
            scope.n_val = x[0];
        { title:"async3",value: 3},
        function(){logger("3 done",scope);async4();}
    function async4(){
        { title:"async4",value: 4},
        function(){logger("4 done",scope);}
  • Thanks for posting!

    Calling next() only seems to work if apply is defined in the global context (not in a module) because = function().... defines the function next at the global level, correct?

    And that makes it impossible to nest async's, right?

  • I tried to nest asyncs some time ago, and failed :-(
    This wasn't important that time, at least for me.
    Anyway, now I have the explanation.

  • It's nice to see the job getting done... for the price of 5 globals... not bad.

    Reduce the 5 to 1 in the spirit of object-orientation helps with clean-up and passing the one as parm to the sequence defining application functions (async1..4) would conribute to hardening.

    I liked the naming in Kazimir's original post: queue... and that it is: queing of sets of asynch operations that have to complete one after the other before a next set is allowed to be entered, and finally chaining these sets.

    In some of my implementatins I needed result-dependent follow-up async operation(s) to be completed before the next set could be entered, and therefore allowed an insertion into or append to the funcs array. The effect is similar to a pseudo-multi-level / quasi-context creation (easy to implement by enhancing the next() to accept optionally the required parameters to do it).

    Surfing the subject shows many implementations with various focus.

    @JumJum, what is the obj's purpose in line 46? Espruino is reporting it undefined...

  • Mhh, I have to read Kazimir's original post...

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

Async handling

Posted by Avatar for JumJum @JumJum