Hillary Namanya Hillary Namanya - 6 months ago 172
Node.js Question

Making and using NodeJS's soap client synchronous

A using NodeJs's current soap client to call a soap service function whenever i send an HTTP request to the server.

The Problem is that the request returns before the soap service call finishes, thus returning no data.

The issues here is that the logic is asynchronous yet i need to make it synchronous.

Please kindly direct me to the right solution.

function soap_get_items(req,res){
var resp = [];

var soap = require('soap');
var url = 'http://example.com/example.wsd?singleWsdl';
var args = {name:"N.Hillary"};

soap.createClient(url, function(err, client) {
client.exampleSoapFunction(args, function(err, result) {
resp = result;
});
});

json_resp = JSON.stringify(resp);
res.end(json_resp);
}

Answer

You dont need to make it synchronous - you need to just use the callback provided:

soap.createClient(url, function(err, client) {
    client.exampleSoapFunction(args, function(err, result) {
        res.end(JSON.stringify(result));
    });
});