Javascript 有没有一种方法可以保证回调方法之外的回调?
首先,我必须为您即将看到的代码暴行道歉 我正在使用NodeJS编写soapweb服务 我的问题是:有没有一种方法可以在多个异步方法完成后执行代码 我的代码如下Javascript 有没有一种方法可以保证回调方法之外的回调?,javascript,node.js,soap,Javascript,Node.js,Soap,首先,我必须为您即将看到的代码暴行道歉 我正在使用NodeJS编写soapweb服务 我的问题是:有没有一种方法可以在多个异步方法完成后执行代码 我的代码如下 var http = require('http'); var soap = require('soap'); var strategyService = { Strategy_Service: { Strategy_Port: { getOptions: function(args, callback) {
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来测试我的服务器,它们似乎以不同的方式传递数据
然后,讨厌的怪兽筑巢,如果其他块。这是因为回调方法是在异步函数的末尾执行的,但在多个异步函数给出响应的情况下,似乎没有办法(我知道)执行函数
我试过的
我首先尝试在不同方法的回调中设置一个变量。比如说
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();
}
);
}
);
}
那个代码确实有效,但它比所有人都要丑,我讨厌它
非常感谢您的任何建议,代码示例对我的帮助最大
如果您对查看所有项目文件感兴趣,可以查看它们。是的,这是可能的!:)
您需要一个控制流库。类似于回调版本或承诺库,比如或Q(甚至ES6承诺)
如果使用async,您需要排列所有要运行的函数,然后等待它们全部在中或中发生(或者在一个名为a的特殊系列中发生,如果您需要结果向下传播)
承诺有点不同,但功能相似 异步回调地狱的答案是承诺。等待多个回调的非承诺答案是计算有多少仍处于活动状态。每次发射时递增计数;在回调中,如果为零,则递减并触发最终计算。(或者,等效但更有力地使用活动xhr数组,启动时推送,回调时弹出,空时处理。)但你完全应该学习承诺,因为它们为你做了所有不可靠的事情。例如,我想补充的是,我在中发现了一个关于javascript承诺的非常好的介绍。它甚至引用,而且碰巧的是,它看起来和我的代码一模一样!