gpt4 book ai didi

javascript - 有没有办法保证回调方法之外的回调?

转载 作者:搜寻专家 更新时间:2023-11-01 00:00:27 26 4
gpt4 key购买 nike

首先,我必须为您即将看到的代码暴行道歉。

我正在使用 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 block 是因为我使用 SoapUI 和 NodeJS 来测试我的服务器,它们似乎以不同的方式传递数据。

然后,嵌套 if else block 的恶心野兽。这是因为回调方法是在异步函数结束时执行的,但似乎没有办法(我知道)在多个异步函数已给出响应的条件下执行一个函数。

我尝试过的

我首先尝试在不同方法的回调中设置一个变量。例如

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 .

最佳答案

是的,这是可能的! :)

您需要一个控制流库。类似于 async对于回调版本或 promise 库,例如 bluebird或 Q(甚至 ES6 promise )。

如果使用异步,您需要排列所有要运行的函数,然后等待它们全部在 parallel 中发生。或在 series (或者在一个名为 waterfall 的特殊系列中,如果您需要结果滴下来)。

Promise 略有不同,但具有相似的功能。

关于javascript - 有没有办法保证回调方法之外的回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35834131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com