Vikram,

Put all your requests into a queue.  In your run() function, shift the
next request off the queue.  If there is nothing, you're done.  If
there is something, process it, and when it's done, call run() again.

You don't need synchronous HTTP.  You also don't really need
control-flow utils or code rewriters, though these things can be
convenient or useful (YMMV).

Example:

```javascript
// request-queue.js
// usage:
// var q = require('./request-queue.js')
// q.add('http://google.com/', googleCallback);
// q.add('http://facebook.com/', facebookCallback);
// q.run(allRequestsDoneCallback);

exports.add = addRequest;
exports.run = run;

var queue = [];

// call the cb when THIS url is done
function addRequest(url, cb) {
  queue.push([url, cb]);
}

// call the cb when ALL requests are done
function run(cb) {
  var next = queue.shift();
  if (!next) {
    if (cb) cb();
    return;
  }
  var url = next[0];
  var reqCb = next[1];
  makeRequest(url, function(er) {
    if (er) {
      if (reqCb) reqCb(er);
      if (cb) cb(er);
      return;
    }
    run(cb);
  });
}

function makeRequest(url, cb) {
  var body = '';
  http.get(url, function(res) {
    // could also do other custom logic in here.
    // for this example, we just buffer the whole body
    // you may want to sniff the responseCode, or stream
    // the body to the caller, etc.
    res.setEncoding('utf8');
    res.on('data', function(c) {
      body += c;
    });
    res.on('end', function() {
      cb(null, body);
    });
    res.on('error', cb);
  });
}
```



On Tue, Feb 12, 2013 at 4:15 AM, asynqronic <c...@asynqronic.com> wrote:
>
>
> On Tuesday, February 12, 2013 6:18:19 PM UTC+7, vikram patil wrote:
>>
>> Thanks but its not known how many requests I am going to make ... e.g. if
>> code reads a file will multiple http request it may or may not pipeline
>> results based on
>> configuration specified in that file. So my implementation is around
>> synchronous http requests and it analyzes result structure using rules
>> specified for  that
>> request and then use result to update input param for next request.
>>
>
> Your problem can be easily solved by making asynchronous requests in
> recursive manner as greelgorke wrote before. But if you insist on
> synchronous syntax you can use synchronize.js library to convert
> asynchronous requests to synchronous.
>
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to