Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.