Ask Your Question

Calling services within a for loop RosLibJS

asked 2017-07-15 01:31:39 -0500

Werewolf_86 gravatar image

updated 2017-07-15 01:42:38 -0500


I was trying to call services from browser using roslibjs. I want to call services within a for loop like this:

for ( int i=0; i<3; i++)
   //wait for acknowledgement 1 
   //wait for acknowledgement 2

I'm able to call the service. But, I don't know how to make Service 1 wait for the acknowledgement before starting the Service2. How do I accomplish this in roslibjs ?

Thanks and regards !

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-07-15 04:04:21 -0500

gvdhoorn gravatar image

I'm not a roslibjs expert, but Javascript by nature is rather event-based, especially when dealing with network traffic. So control flow is not defined by the order of statements in your script, but by the dataflow (ie: funcA() sends out a request, registers funcB() to be called whenever the result arrives, etc).

Looking at the documentation for the Service client class, the following is the spec: callService(request, callback, failedCallback).

Putting all this together, you could get the control flow you're after by passing a function for the callback argument that receives the result of your service1, and after inspecting it (ie: is everything ok), invokes callService2(). To make things more robust, you might also want to register a failedCallback, and put appropriate error handling there.

Note that this models only a single iteration of your for-loop. You could set up a sort-of 'ping-pong' between two callbacks that call callService2() and callService1() respectively.

Asynchronous, event-based programming is slightly different, but very doable once you get used to it.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2017-07-15 01:31:39 -0500

Seen: 337 times

Last updated: Jul 15 '17