Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1102045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:07:23+00:00 2026-05-17T01:07:23+00:00

I am about to spend a lot of time solving a problem, and I

  • 0

I am about to spend a lot of time solving a problem, and I wonder if there is an existing recipe for this. It’s a browser-based application (JavaScript and Dojo Toolkit) that is a client of a RESTful Web Service.

It uses Comet to automatically update the display. There is a callback function that processes every message received. [Boring details of Comet: as a sort of background operation, an HTTP request is made to the server. This request blocks on the server until it has a message for us. When the client finally receives the response, it calls the callback function and then makes the next HTTP request. HTTP allows up to two simultaneous requests, so this “background” request does not block the “foreground” requests that happen when the user does things.]

There is an adapter layer below the UI layer. The UI layer thinks it’s getting messages pushed to it. The adapter layer is doing the Comet requests and transforming the response from what the server sends to what the UI layer expects.

var ourEventFilter = dojo.hitch(this, function(evt) {
  if (evt["obj"]) {
        evt.obj = this.transform(evt.obj);
  }
  callUIEventHandler(evt);
}

[dojo.hitch() is syntactic sugar to make a closure, binding a function’s this.]

The obj may look something like this:

{
   "resources": [
      {"name":"Me", "type":"vm", "link":"http://server/item/ABC"},
      {"name":"You", "type":"real", "link":"http://server/item/123"}],
   "subObjs": [
      "resources":[{"name":"Him", "type":"vm", "link":"http://server/item/DEF"} 
   ]
}

The transform function turns it into this:

{
   "resources": [
      {"name":"You", "type":"real", "link":"http://server/item/123"},
  ],
   "vms": [
      {"name":"Me", "type":"vm", "link":"http://server/item/ABC"}],
   "subObjs:" [
      "resources":[],
      "vms": [{"name":"Him","type":"vm", "link":"http://server/item/DEF"}]
   ]
}

We find those “resources” that are of type “vm” and move them to a separate array. So far so good. The transform function is simple enough. It’s recursive because subOjbs can contain subObjs.

But now we need more information on the vms. We need to make Ajax calls to the server to get this information:

{
   "resources": [
      {"name":"You", "type":"real", "link":"http://server/item/123"}],
   "vms": [
      {"name":"Me", "type":"vm", "link":"http://server/item/ABC", "moreInfo":"X"}],
   "subObjs:" [
      "resources":[],
      "vms": [{"name":"Him","type":"vm", "link":"http://server/item/DEF", 
                "moreInfo":"Y"}]
   ]
}

The transform function looks something like this:

transform: function(obj) {
      var vms=[];
      var newResources = [];
      // Recurse on subObjs
      if (obj.subObjs) {
         for (var kx = 0; kx < obj.subObjs.length; kx++) {
            ojb.subObjs[kx] = this.transform(obj.subObjs[kx]);
      }
      // Move vms out of resources into vms
      if (obj.resources) {
         for (var jx = 0; jx < obj.resources.length; jx++) {
            if (obj.resources[jx].type == "vm") {
               var thisVM = obj.resources[jx];
               // Note:  more info needed here.
               //thisVM = this.getMoreInfo(thisVM);
               vms.push(thisVM);
            } else { 
               newResources.push(obj.resources[jx];
            }
         }
         obj.vms = vms;
         obj.resources = newResources;
      }
      return obj;
}

And now we have a problem. How do I wrote getMoreInfo()?

I could make synchronous calls at this point:

getMoreInfo: function(vm) {
   vmObj = callServerSynchronouslyToGET(vm.link);
   vm.moreInfo = vmObj ? vmObj.moreInfo : null;
}

But synchronous calls are never a good idea in Ajax (it would be Sjax).

I don’t think it’s possible to write getMoreInfo() as such to make asynchronous calls. I have to go back up through several layers of the onion and rewrite everything from a certain point downward, I hope without rewriting anything above the Comet-callback layer.

I know a recipe that transforms a recursive function into a non-recursive function. Is there a recipe that transforms an onion with a loop with synchronous GET at the center into a chain of asynchronous GETs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:07:24+00:00Added an answer on May 17, 2026 at 1:07 am

    There is a cross-browser extension to the JavaScript language called StratifiedJS.

    It is designed to solve precisely the problem you are mentioning: It allows you to program in a synchronous style with everything being executed asynchronously under the hood.

    The JS library that enables StratifiedJS on the browser is called “Oni Apollo”. See http://onilabs.com/apollo for more details.

    In your particular case, you could convert your whole asynchronous onion into synchronous code using StratifiedJS or you could keep your existing logic by just sticking it into a “text/sjs” script element:

    <script src="http://code.onilabs.com/latest/oni-apollo.js"></script>
    
    <script type="text/sjs">
    
      // your existing code here
    
      getMoreInfo: function(vm) {
        var vmObj = require('http').get(vm.link);
        vm.moreInfo = vmObj ? vmObj.moreInfo : null;
      }
    </script>
    

    Here, require(‘http’).get() performs an asynchronous XHR under the hood (for details, see the apollo api docs at the above link).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already spent a lot of time trying to solve this problem, I've seen
Have spend quite some time to learn Sencha touch, just like an evaluation about
This problem is about concurrent high speed inserts. I must admit it is very
while profiling NHibernate with NHProf I noticed that a lot of time is spend
I spent a lot of time recently reading about debugging. One of the aspects
Hey, I spent A LOT of time learning about Android coding and I finally
I do not want to spend a lot of time on something that does
I spend a lot of my time in emacs, and for the most part
I spend about 2 hours reading a lot of factory related topics and I
I have spent about half a day searching for an answer to this question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.