Gmail is not a very good code editor.  Did you find the bug? ;)

On Tue, Feb 12, 2013 at 2:01 PM, Isaac Schlueter <i...@izs.me> wrote:
> ```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, body) {
     if (er) {
       if (reqCb) reqCb(er);
       if (cb) cb(er);
       return;
     }
     if (reqCb) reqCb(null, body);
     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