javascript - NodeJS NPM soap - 我如何在没有回调的情况下链接异步方法(即使用异步或承诺)?
问题描述
我已经使用nodejs/javascript成功调用了一系列soap webservice方法,但是使用回调......现在它看起来像这样:
soap.createClient(wsdlUrl, function (err, soapClient) {
console.log("soap.createClient();");
if (err) {
console.log("error", err);
}
soapClient.method1(soaprequest1, function (err, result, raw, headers) {
if (err) {
console.log("Security_Authenticate error", err);
}
soapClient.method2(soaprequest2, function (err, result, raw, headers) {
if (err) {
console.log("Air_MultiAvailability error", err);
}
//etc...
});
});
});
我正在尝试使用 Promise 或异步来获得更清洁的东西,类似于此(基于此处https://www.npmjs.com/package/soap文档中的示例):
var soap = require('soap');
soap.createClientAsync(wsdlURL)
.then((client) => {
return client.method1(soaprequest1);
})
.then((response) => {
return client.method2(soaprequest2);
});//... etc
我的问题是,在后一个示例中,soap 客户端在第一次调用后不再可访问,它通常返回“未定义”错误......
是否有一种“干净”的方式通过这种链接来携带对象,以便在后续调用中使用/访问?
解决方案
使用async/await
语法。
const soap = require('soap');
(async () => {
const client = await soap.createClientAsync(wsdlURL);
cosnt response = await client.method1Async(soaprequest1);
await method2(soaprequest2);
})();
注意Async
两者createClient
和method1
推荐阅读
- php - 在渲染之前使用 PHP 替换 HTML 页面中的信息
- javascript - Ag-Grid - 访问所有数据 - 包括计算列
- javascript - 如何设置和获取加载圈@keyframe的值
- c# - 如何在 @Html.RadioButtonFor 上获取图像
- javascript - Sitefinity 编辑器立即自行关闭
- mongodb - 如何在 mongoDB 中使用 RRD4J?
- r - 有没有办法为每次调用 lapply 更新参数?
- sql - 在 SQL Server 函数中动态转换
- java - 使用 LinkedHashMap 到列表时映射值不正确
- macos - 模拟 docker 容器之间的网络请求超时