首页 > 解决方案 > 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 客户端在第一次调用后不再可访问,它通常返回“未定义”错误......

是否有一种“干净”的方式通过这种链接来携带对象,以便在后续调用中使用/访问?

标签: javascriptnode.jssoap

解决方案


使用async/await语法。

const soap = require('soap');

(async () => {
const client = await soap.createClientAsync(wsdlURL);
cosnt response = await client.method1Async(soaprequest1);
await method2(soaprequest2);
})();

注意Async两者createClientmethod1


推荐阅读