最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS NPM soap - how do I chain async methods without callbacks (ie use async or Promise)? - Stack Overflow

programmeradmin5浏览0评论

I have successfully called a sequence of soap webservice methods using nodejs/javascript, but using callbacks... right now it looks something like this:

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

});

I'm trying to get to something cleaner using Promise or async, similar to this (based on the example in the docs here ) :

var soap = require('soap');

soap.createClientAsync(wsdlURL)
    .then((client) => {
        return client.method1(soaprequest1);
    })
    .then((response) => {
        return client.method2(soaprequest2);
    });//... etc

My issue is that in the latter example, the soap client is no longer accessible after the first call and it typically returns a 'not defined' error...

is there a 'clean' way of carrying an object through this kind of chaining to be used/accessible in subsequent calls ?

I have successfully called a sequence of soap webservice methods using nodejs/javascript, but using callbacks... right now it looks something like this:

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

});

I'm trying to get to something cleaner using Promise or async, similar to this (based on the example in the docs here https://www.npmjs./package/soap) :

var soap = require('soap');

soap.createClientAsync(wsdlURL)
    .then((client) => {
        return client.method1(soaprequest1);
    })
    .then((response) => {
        return client.method2(soaprequest2);
    });//... etc

My issue is that in the latter example, the soap client is no longer accessible after the first call and it typically returns a 'not defined' error...

is there a 'clean' way of carrying an object through this kind of chaining to be used/accessible in subsequent calls ?

Share Improve this question asked Jun 28, 2019 at 6:34 MetaSimianMetaSimian 632 silver badges5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 16

Use async/await syntax.

const soap = require('soap');

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

Pay attention to Async on both createClient and method1

In order to keep the chain of promises flat, you can assign the instance of soap to a variable in the outer scope:

let client = null;

soap.createClientAsync(wsdlURL)
  .then((instance) => {
    client = instance
  })
  .then(() => {
    return client.method1(soaprequest2);
  })
  .then((response) => {
    return client.method2(soaprequest2);
  });

Another option would be nested chain method calls after the client is resolved:

soap.createClientAsync(wsdlURL)
  .then((client) => {
    Promise.resolve()
      .then(() => {
        return client.method1(soaprequest2);
      })
      .then((response) => {
        return client.method2(soaprequest2);
      });
  })

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论