制作和使用NodeJS的soap客户端同步

当我向服务器发送一个HTTP请求时,使用NodeJs的当前soap客户端来调用soap服务函数。

问题是请求在soap服务调用完成之前返回,因此没有返回数据。

这里的问题是,逻辑是asynchronous的,但我需要使其同步。

请亲切指导我到正确的解决scheme。

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); } 

您不需要使其同步。 你只需要使用提供的callback:

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