NodeJS NPM soap-如何链接不带回调的异步方法(即使用异步或Promise)?

Met*_*ian 0 javascript soap node.js

我已经使用nodejs / javascript成功调用了一系列肥皂网络服务方法,但是使用了回调...现在看起来像这样:

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... 
        });
    });

});
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用Promise或async来达到更清洁的效果,类似于(基于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
Run Code Online (Sandbox Code Playgroud)

我的问题是,在后一个示例中,肥皂客户端在第一次调用后将不再可访问,并且通常会返回“未定义”错误...

是否有一种“干净”的方式通过这种链接携带物体以在随后的调用中使用/访问?

Usa*_*hir 6

使用async/await语法。

const soap = require('soap');

(async () => {
const client = await soap.createClientAsync(wsdlURL);
cosnt response = await client.method1(soaprequest1);
await method2(soaprequest2);
})();
Run Code Online (Sandbox Code Playgroud)

  • 这应该是选择的答案。 (2认同)