1

I have a code that looks like this:

function program(){
  this.bye = function(){
     //some code
     return this;
  }

  this.hello = function(){
     if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(geoSuccess,geoError);
     }
     return this;
  }

  if(this instanceof program()){
     return this.program;
  } else {
     return new program();
  }

}

var PR = new program();

My question is this: How can I accomplish a chain execution such as:

TB.hello().bye();

and execute bye() when the geolocation response arrives.

I would like to keep the chain execution without having to execute .bye() from inside geoSuccess function. Is there any way to accomplish this ?

0x_Anakin
  • 3,229
  • 5
  • 47
  • 86
  • 1
    Using a [promise](http://wiki.commonjs.org/wiki/Promises/A)? (Which, obviously, won't just magically happen, you'd have to do additional coding though there are some lightweight promise libraries that can help you.) – nnnnnn Jul 12 '13 at 11:56
  • You have to work with the callback `geoSuccess` as this is asynchronous. – Amberlamps Jul 12 '13 at 11:56
  • Yes I'm aware of geoSuccess being asynchronous. but I have no idea on how to keep the chaining. Any references or examples that could help ? – 0x_Anakin Jul 12 '13 at 12:03
  • 1
    @PanagiotisGeorgeRaditsas: Promises is the way to go then! – Amberlamps Jul 12 '13 at 12:04
  • 1
    I think you want to do this: http://stackoverflow.com/a/14377406/1587864 – margabit Jul 12 '13 at 12:09

0 Answers0