user1076861
user1076861

Reputation: 1

how to wait for the callback function to get response in node.js

When my callback function parse the wsdl file and give the response to me I want to show html page in which i want to show list view with parsed data from my node.js request handler.Here is my code,

soap.createClient(AW_URL, function(err, client) {
  if (err) {
    console.log(err.stack);
    return;
  } 
  else {
    client.setSecurity(new soap.WSSecurity(auth.login, auth.key));
    client.ListProviders(function(err, res) {
         if (err) {
              console.log(err.stack);
              return;
         }
         else {
              var pro = res.Providers[1];
              console.log(pro);

         }
    });
  }
});

//var body = html page

    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(body);
    response.end();

so i am stuck with how to wait for the soap client callback function to get the data because before the data come to me html get in-front.Need a help for this.Any suggestion is great help for me.Thanks in advance.

Upvotes: 0

Views: 2117

Answers (2)

Kato
Kato

Reputation: 40582

You haven't actually specified where you want the html to get generated. All the client methods accept a callback (you have incorrectly put the callback into the args object spot for client.ListProviders.

Let's assume you wanted client.ListProviders to execute before your response gets written...

// where is `response` getting set?? it's not in your example
var response;

function callback() {
   // do stuff here
   response.writeHead(200, {"Content-Type": "text/html"});
   response.write(body);
   response.end();
}

soap.createClient(AW_URL, function(err, client) {
   if (err) {
      //errors
   }
   else {
      client.ListProviders({x: 2, y: 4}, callback);
   }
});

However, I'm not really sure what the issue is. Presumably your soap call is going to output some html? So you probably want something more like this:

// where is `response` getting set?? it's not in your example
var response;
var headerHtml = '<h1>Hello World!</h1>';
var footerHtml = '<div>I like swords!</div>';

// create whatever goes above the soap call
response.writeHead(200, {"Content-Type": "text/html"});
response.write(headerHtml);


function callback(err, result) {

   // do some stuff that create's html and call response.write();

   finishPage();
}

function finishPage() {
   response.end(footerHtml);
}

soap.createClient(AW_URL, function(err, client) {
   if (err) {
      //errors
   }
   else {
      client.ListProviders({x: 2, y: 4}, callback);
   }
});

Upvotes: 0

Jan Jongboom
Jan Jongboom

Reputation: 27313

Program flow in node.js works like:

response.writeHead(200, { "Content-Type": "text/html" });

doSomethingAsync(function (err, res) {
    response.write(res);
    response.end();
});

So, you don't have to call end() immediately. You can call it from any callback, then the behavior will be as expected.

Upvotes: 1

Related Questions