最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nested requests are blocking - Stack Overflow

programmeradmin2浏览0评论

I am relatively new to nodejs. I've been recently pooling all of the collective knowledge i've gathered through the past couple of months into an project. I believe I've ran into my first "blocking" issue in nodejs.

I have a page that loads two request() calls they are async and nested accordingly. The innermost one uses data from the innermost to redirect the user.

  request(parameters,function(error, response, data){
      //the first request passes a token  
      request(newParamters,function(error, response, data){
          //the second request passes a url
          res.redirect(data.info.url);
      });
  });

The error is that when I open this up in many browser tabs it ends up breaking after the first couple then the server says data.info.url is undefined.

My question to you is: Should I only be performing one request at a time? I could save the token from the first request() and redirect the user to the second request() would this help? I've been very conscience about async and not blocking and I'm shocked that this is happening. Any feedback would be great!

I am relatively new to nodejs. I've been recently pooling all of the collective knowledge i've gathered through the past couple of months into an project. I believe I've ran into my first "blocking" issue in nodejs.

I have a page that loads two request() calls they are async and nested accordingly. The innermost one uses data from the innermost to redirect the user.

  request(parameters,function(error, response, data){
      //the first request passes a token  
      request(newParamters,function(error, response, data){
          //the second request passes a url
          res.redirect(data.info.url);
      });
  });

The error is that when I open this up in many browser tabs it ends up breaking after the first couple then the server says data.info.url is undefined.

My question to you is: Should I only be performing one request at a time? I could save the token from the first request() and redirect the user to the second request() would this help? I've been very conscience about async and not blocking and I'm shocked that this is happening. Any feedback would be great!

Share Improve this question edited Aug 25, 2012 at 5:01 ThomasReggi asked Aug 24, 2012 at 0:11 ThomasReggiThomasReggi 59.7k97 gold badges259 silver badges459 bronze badges 2
  • 1 Is there really a closing parenthesis after parameters? – Joseph Commented Aug 24, 2012 at 0:25
  • where does this newParameters e from? is it from the previous request? can you post a more concise code? – Joseph Commented Aug 24, 2012 at 1:20
Add a ment  | 

1 Answer 1

Reset to default 7

Nesting like this is going to lead to many problems.

I prefer this pattern, way I break-up everything to named functions that are much easier to read.

When a function pletes it calls the next and so on.

parameters = {};   //define parameters
first(parameters); // call first function to kick things off.

var first = function(parameters) {

      request(parameters,function(error, response, data){
         newParamters = date.something;        
         second(newParamters, data); //call second function         
     });
};

var second = function(newParamters, data){

      request(newParamters,function(error, response, data){
          res.redirect(data.info.url);
     });
}

This pattern is "non-blocking", so when the first request is made the nodejs process exits and continues when the response is received only then will the second function get called.

When the second request is made the nodejs process exits again. The redirect will only occur after the response is received.

发布评论

评论列表(0)

  1. 暂无评论