Copyright © https://mongoose-os.com

Mongoose OS Forum

frame
ATTENTION! This forum has moved to:

https://community.mongoose-os.com

Do not post any new messages.

Not able to wrap the callback using mJS engine

Hi

I am trying to write a simple js that wraps the callback with another function. For example:

function my_callback() {
  print("helloWorld");
}

function wrapper(callback) {
  return function() {
    print("b4 helloWorld");
    callback();   // getting error on this line
    print("8fer helloWorld");
  }
}

Timer.set(1000, true, wrapper(my_callback), null);

However, I am getting error saying the callback cannot be found in the wrapper function. Is this a bug or limitation of the mJS engine? Any pointer is appreciated. Thanks!

Comments

  • My understanding is it should be as follows

    let my_callback = function(){
    print("helloWorld");
    }

    and in your timer you have

    Timer.set(1000, true, my_callback, null);

  • SergeySergey Dublin, Ireland
    edited August 2017

    See mjs limitations at https://github.com/cesanta/mjs#restrictions

    You cannot return a function. Nested scoping is OK, but closures are not implemented.

    This works:

    Timer.set(1000, true, function() { print(Timer.now()); }, null);
    
Sign In or Register to comment.