Tuesday 9 May 2017

How to make a FAST synchronous request on Node.js

I have converted a program using synchronous(http request) call from client side(using ajax) to server side(Nodejs). After that, the program takes 4 times longer than it took.

I get 'undefined' as return of the function when I use asynchronous call. So, I have tried two ways of synchronous call, and both takes too long time.

Is there good ways to get 'body_return' in the function below, using async call? Or, using FAST sync call?

function getBody(input) {

  //sync call-TRY.1
  var body_return;
  request(option, function(error, response, body) {
    if (!error && response.statusCode === 200) {

       //do something with body;
       body_return = dosomething(body);
     }
  });

  //sync call-TRY.2
  var body = sync_request('POST', '(uri)', options).getBody('utf8');
  var body_return = dosomething(body);

  //async call can't return the body in time, so this function returns undefined..

  return body_return;
}



via hyewon330

No comments:

Post a Comment