cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

Return in the middle of a service

ngarner
1-Visitor

Return in the middle of a service

Hello,

I'm writing a service where I need to exit/return execution based on a programmatic decision.  For example:

if (var < 10)

{

  return/exit

}

continue execution

Thingworx throws an error when saving a service if you put "return" in the code.  What's the proper way to exit a service in the middle of execution?

Thank you,

Nick

6 REPLIES 6

There's no way of doing it, you need to put on if/else conditions.

For some special cases I use a "fake" throw exception:

throw "MESSAGE";

But I don't recommend you on using it, as it generates nasty logger messages, it would be good that they implement a way of doing both:

  • Return before the end of the service
  • Throw Exceptions

Carles.

vmihai
12-Amethyst
(To:ngarner)

Hi,

I usually handle this by doing everything that I want to do in the if. So if the condition is not met, the service simply doesn't do anything and the call ends.

In your situation I would use

if ( var > 10 )

{

     // implement your code here

}

I also usually return some error messages if I need to show UI feedback, when the condition is not met. For example when I need to validate if all required fields have been inputted in a  form created with ThingWorx.

Hope this helps,

Veronica

PaiChung
22-Sapphire I
(To:ngarner)

Also consider try/catch/throw (besides if/else)

wrap the service body in an immediately invoked function.

original:

do stuff

if (my_var < 10)

{

     return; // fails

}

continue execution

wrapped:

var result = (function() {

     do stuff

     if(my_var < 10)

     {

          return; // works now.

     }

     continue execution

})();

ngarner
1-Visitor
(To:ngarner)

Great, thank you, everyone.

Nick

Hi,

I've discovered another way with javascript of doing it:

labelName:

{

      // -- your code

      break labelName; // -- it will jump until the end of the brakets.

     // -- other conditional code

}


Best Regards,

Carles.

Announcements


Top Tags