首先,我必须为你将要看到的代码残暴道歉。
我正在使用SOAP module在NodeJS中编写SOAP Web服务。
我的问题是:在多个异步方法完成后有没有办法执行代码?
我的代码如下
var http = require('http');
var soap = require('soap');
var strategyService = {
Strategy_Service: {
Strategy_Port: {
getOptions: function(args, callback) {
var source = "";
var destination = "";
var taxi = false;
var shuttle = false;
var bus = false;
var taxiResponse = "N/A";
var shuttleResponse = "N/A";
var busResponse = "N/A";
if (args.source.$value != undefined){
source = args.source.$value;
destination = args.destination.$value;
taxi = getBoolean(args.taxi.$value);
shuttle = getBoolean(args.shuttle.$value);
bus = getBoolean(args.bus.$value);
} else {
source = args.source;
destination = args.destination;
taxi = getBoolean(args.taxi);
shuttle = getBoolean(args.shuttle);
bus = getBoolean(args.bus);
}
var url;
var args = {"tns:source":source, "tns:destination":destination};
if (taxi){
url = "http://localhost:8001/wsdl?wsdl";
soap.createClient(url, function(err, client){
client.Taxi_Service.Taxi_Port.takeTaxi(args, function(err, result){
if (err) throw err;
taxiResponse = result.message.substring(0, result.message.length);
if (!bus && ! shuttle){
callback({
taxi: taxiResponse,
bus: busResponse,
shuttle: shuttleResponse
});
}
if (bus){
url = "http://localhost:8003/wsdl?wsdl";
soap.createClient(url, function(err, client){
client.Bus_Service.Bus_Port.takeBus(args, function(err, result){
if (err) throw err;
busResponse = result.message.substring(0, result.message.length);
if (!shuttle){
callback({
taxi: taxiResponse,
bus: busResponse,
shuttle: shuttleResponse
});
}
if (shuttle){
url = "http://localhost:8002/wsdl?wsdl";
soap.createClient(url, function(err, client){
client.Shuttle_Service.Shuttle_Port.takeShuttle(args, function(err, result){
if (err) throw err;
shuttleResponse = result.message.substring(0, result.message.length);
callback({
taxi: taxiResponse,
bus: busResponse,
shuttle: shuttleResponse
});
})
})
}
})
})
} else if (shuttle){
url = "http://localhost:8002/wsdl?wsdl";
soap.createClient(url, function(err, client){
client.Shuttle_Service.Shuttle_Port.takeShuttle(args, function(err, result){
if (err) throw err;
shuttleResponse = result.message.substring(0, result.message.length);
callback({
taxi: taxiResponse,
bus: busResponse,
shuttle: shuttleResponse
});
});
});
}
});
});
} else if (bus){
url = "http://localhost:8003/wsdl?wsdl";
soap.createClient(url, function(err, client){
client.Bus_Service.Bus_Port.takeBus(args, function(err, result){
if (err) throw err;
busResponse = result.message.substring(0, result.message.length);
if (!shuttle){
callback({
taxi: taxiResponse,
bus: busResponse,
shuttle: shuttleResponse
});
}
if (shuttle){
url = "http://localhost:8002/wsdl?wsdl";
soap.createClient(url, function(err, client){
client.Shuttle_Service.Shuttle_Port.takeShuttle(args, function(err, result){
if (err) throw err;
shuttleResponse = result.message.substring(0, result.message.length);
callback({
taxi: taxiResponse,
bus: busResponse,
shuttle: shuttleResponse
});
})
})
}
});
});
} else if (shuttle){
url = "http://localhost:8002/wsdl?wsdl";
soap.createClient(url, function(err, client){
client.Shuttle_Service.Shuttle_Port.takeShuttle(args, function(err, result){
if (err) throw err;
shuttleResponse = result.message.substring(0, result.message.length);
callback({
taxi: taxiResponse,
bus: busResponse,
shuttle: shuttleResponse
});
});
});
} else {
callback({
taxi: taxiResponse,
bus: busResponse,
shuttle: shuttleResponse
});
}
}
}
}
}
var xml = require('fs').readFileSync('StrategyService.wsdl', 'utf8'),
server = http.createServer(function(request,response) {
response.end("404: Not Found: "+request.url)
});
server.listen(8000);
soap.listen(server, '/wsdl', strategyService, xml);
getBoolean = function(string){
lowerCase = string.toLowerCase();
if (lowerCase == "true" || lowerCase == "t" || lowerCase == "1"){
return true;
} else {
return false;
}
}
在我的辩护中,我会说这是我第一次尝试使用NodeJS创建SOAP服务,我对NodeJS也是新手。
第一个if else块是因为我使用SoapUI和NodeJS来测试我的服务器,它们似乎以不同的方式传递数据。
然后,嵌套if else块的令人厌恶的野兽。这是因为回调方法是在异步函数结束时执行的,但似乎没有办法(我意识到)在多个异步函数给出响应的条件下执行函数。 / p>
我首先尝试在不同方法的回调中设置变量。例如
fun1(args, callback){
fun1Finished = true;
callback();
}
fun2(args, callback){
fun2Finished = true;
callback();
}
while (!fun1Finished || !fun2Finished){
// code gets stuck here until fun1 and fun2 are finished
}
fun3(args, callback){
// do stuff that requires both functions to be finished
callback();
}
那不起作用,所以我决定编写一系列嵌套回调,例如
fun1(args, callback){
// do stuff
callback(
// now fun2 will be executed after fun1
fun2(args, callback){
// do more stuff
callback(
// now fun3 will be executed after fun2, which was executed after fun1
fun3(args, callback){
// do stuff that requires both fun1 and fun2 to be finished
callback();
}
);
}
);
}
该代码确实有效,但它比所有人都更加丑陋,我讨厌它。
我们非常感谢任何建议,而且代码示例对我的帮助更大。
如果您有兴趣查看所有项目文件,可以查看它们on my github。