Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am currently working on FLEX and have to call a web services. once I have the result, I have to call the web service again, with the previous result as the input.

A compounding problem, is that the service might return multiple results, and I have to call the service again with each of the results as the input. Lastly, i need the results to be in a hierarchical array collection, with each result as the child of the input to the web service.

If it were a synchronous function, it would have been very easy, but I have spent half a day trying to come up with something elegant for the web service.

I ended up with having two arrays, one for the input, and one for the results. Every time I had one or more result, I would put it in the Input array and the results. I would then remove the first item from the input array and call the web service with it, and so on, till I finished all the inputs. Once I had all the results, I made a recursive function that would create the hierarchical tree, that I needed.

This seems like a very hacky, WTF Solution. Is there any elegant and better solution? I am all ears.

share|improve this question
If this question seems to be better suited for stackoverflow, mods, please migrate it there. – Devdatta Tengshe Oct 20 '11 at 14:44
It's not a Stack Overflow question - primarily because there's no code! Design and algorithm questions are definitely on topic here. – ChrisF Oct 20 '11 at 14:47
Maybe some kind of Queueing setup would help? – Daenyth Oct 20 '11 at 14:48
@Daenyth The input and result arrays that I mentioned, are a form of a queue. Or am I missing something, and you are talking of something else? – Devdatta Tengshe Oct 20 '11 at 14:51
@DevdattaTengshe: I was talking more about using some kind of library to manage it rather than doing it by hand - such a library may have some kind of provisions for a situation like yours – Daenyth Oct 20 '11 at 15:35
up vote 2 down vote accepted

Maybe you could implement some sort of "auto-loading tree".

Start with the first node. This node fetches data from server. For each item found in the response, it creates a child node that will load the appropriate data. And so on...

One thing you need to know is "When is it complete ?"

This can be handled with events. Each node send an event up when all its children are complete. Recursively this will end up telling the root that all is children are complete. The root node will fire the "I'm complete" event to tell whoever wants to know it, that the tree is complete.

However, I'm wondering : why don't you create a service on the server that returns the whole tree ?

share|improve this answer
The webservice I am consuming, is the public API of a well known web site. If I can call it recursively, It would be possible for my App to have no server side code, since all my code would run in the user's flash client. – Devdatta Tengshe Oct 20 '11 at 15:50
Based on how it's set up, events aren't even necessarily needed for completion-testing. Each node can increment an internal counter when it starts its children, and the child nodes that do the AJAX call to populate themselves can have a callback to the parent. Upon AJAX completion, the child node uses the callback, which decrements the parent's internal counter, and if it hits zero, the parent does the same for its own parent, all the way up the line. – Izkata Oct 20 '11 at 18:19
@Izkata : Devdatta is using Flex. In Flex, it is common practice to use events instead of callbacks. – David Oct 24 '11 at 6:48
Hm, didn't know that (never used Flex and was thinking more generally). Alrighty then – Izkata Oct 24 '11 at 13:58

I also have a series of web based services which need to be called in order. I solved the issue with AJAX. The key is having a callback mechanism to perform the recursion, forcing synchronization.

Something like this (jQuery)

function Recurse(params)
            function (data) {Recurse(data)}//callback does recursive call, also passes in variable data received from the web service

You should check the API in question to see if it is a tree data structure like the DOM. Usually recursion is a good approach to iterate trees. If it's just a flat, linear API then a Queue/Stack can be used to push/pop your way through the API recursively.

Here's how to recursively iterate the DOM. Code for another Tree based API structure will be similar (javascript)

function walk(node, func) {
   //walk the height of the tree
   func(node);//do something to the node
   node = node.firstChild;
   while(node) {
      //walk siblings
      walk(node, func);
      node = node.nextSibling;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.