- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
首先,我必须为您即将看到的代码暴行道歉。
我正在使用 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/
我正在研究 learnyounode 的 HTTP 客户端作业。 我想知道为什么控制台记录来自response.on(“end”,callback)的数据仅输出预期输出的最后一部分,而控制台记录来自r
我正在尝试创建一个对象列表(在我的示例中为 List),我在其中使用 json 将对象添加到此列表,但该列表仍为空。这是我的代码: public List readCardsFromJson() {
我有一个 JavaScript 函数“print_something”,它在大约 300 个 jsp 帮助页面中实现。我发现这个“print_something”函数必须被纠正。所以我正在寻找一个不更
有 2 个 HTML 下拉列表,一个用于 12 小时时间,一个用于每小时 5 分钟的时间间隔。 .. 1 .. 12 .. 0 .. 55 .. 一直在尝试使用 if/
我有一个 A 类,我打算在它与设备驱动程序交互时将其放入共享库中。 我有一个 B 类,将来可能是 C、D、E...,它将使用共享库中的 A 类。 我想要在类 A 中设置回调函数的功能,以便当特定事件发
我需要能够在处理完 Observable.next() 之后执行回调。 我有一个组件“A”,它有一个主题使用 Subject.next() 发送通知。我有一个组件“B”,它订阅了 Subject.as
我有一张在顶部和底部单元格下方带有阴影的表格(此处使用 Matt Gallagher 的解决方案:http://cocoawithlove.com/2009/08/adding-shadow-effe
有人可以向我解释一下为什么这段代码有效 renderSquare(i) { return ( this.handleClick(i)} /> ); } 但
我可以让两个不同的客户端监听相同的 WCF 回调并让它们都接收相同的数据而不必进行两次处理吗? 最佳答案 不是真的 - 至少不是直接的。你所描述的听起来很像发布/订阅模式。 WCF 服务基本上在任何给
我是 SignalR 的新手,如果这个问题太明显,我深表歉意,但我在文档中找不到任何答案。 这是我的代码。 /*1*/ actions.client.doActionA = function (r
我有这个应用程序,您可以在其中输入一些文本并按下一个按钮,将此文本添加到自定义小部件中。这是代码: import 'dart:core'; import 'package:flutter/materi
我读到当您还想使用模型回调时不能使用 Keras 进行交叉验证,但是 this post表明这毕竟是可能的。但是,我很难将其纳入我的上下文。 为了更详细地探讨这个问题,我正在关注 machinelea
我尝试在重力表单中提交表单失败后运行一些 jQuery 代码,也就是验证发现错误时。 我尝试使用 Ajax:complete 回调,但它根本不触发。 我尝试运行的代码基本上将监听器添加到选择下拉列表中
我有一个 $image,我 .fadeIn 和 .fadeOut,然后 .remove .fadeOut 完成。这是我的代码: $image .fadeIn() .fadeOut(func
我正在处理一个自定义文件路径类,它应该始终执行一个函数 写入相应的系统文件及其文件对象后 关闭。该函数将文件路径的内容上传到远程位置。 我希望上传功能完全在用户的幕后发生 透视,即用户可以像使用其他任
这里是 javascript 新手,所以回调在我的大脑中仍然有点不确定。 我想做的是:给定一个“菜单”,它是一个 objectId 数组,查询与该 objectId 相对应的每个 foodItem,获
我正在学习回调,我编写了以下代码: var http = require('http'); var str = ""; var count = 2; function jugglingAsync(ca
这是我的困境,我有一系列被调用的函数,我正在使用回调函数在它们完成时执行函数。回调返回一个值并且效果也很好,我的问题是当我向回调添加参数时我无法再访问返回值。这是一个有效的例子: function m
This question already has answers here: Explanation of function pointers (4个答案) 上个月关闭。 如何将函数指针作为参数传递
我无法让以下代码工作。假设 ajax 调用有效,并且 msg['username'] 预设为 'john'。我想我对如何将变量传递给回调感到困惑。编辑:我认为我的主要困惑是如何从 Ajax 中获取“m
我是一名优秀的程序员,十分优秀!