0

Hi All this is my first qestion on stackoverflow. I wrote a fuction that expect result from soapClient call in nodejs, but it dont wait for the call return, and the return value is empty.

I try several modification of the code but i cant get the expected result. I expect some output but it is empty.

Note URLS are fictitious!!

Thanks somebody can help me!!


import * as soap from "soap";

function getTokens(service: string): String {

    let Auth = "";

    function Call(err: any, client: soap.Client) {

        client.loginCms({ in0: service }, function (
            err: any,
            result: any,
            _rawResponse: string, 
            _soapHeader: any,
            _rawRequest: string
        ) {
            if (err) {
                console.error(err);
             } else {
                Auth = result.loginCmsReturn;
             }
         });
    }

    soap.createClient("wsdl_url", {
        disableCache: true,
        envelopeKey: "env"
    }, Call, "endpoint_url");

    // here return an empty value
    return Auth;
};

const Auth = getTokens("service");
console.log(Auth); // empty

Gustavo
  • 3
  • 2

1 Answers1

1

I recommend you to use promises like in this example taken from the soap package documentation.

The code you have the is not asynchronic, so it doesn't wait for the operation to be completed.

It should look something like this

import * as soap from "soap";

function getTokens(service: string): Promise<string> {

    return new Promise<string>((resolve, reject) => {

        function Call(err: any, client: soap.Client) {

            client.loginCms({ in0: service }, function (
                err: any,
                result: any,
                _rawResponse: string, 
                _soapHeader: any,
                _rawRequest: string
            ) {
                if (err) {
                    console.error(err);
                    reject(err)
                 } else {
                    Auth = result.loginCmsReturn;
                    resolve(Auth);
                 }
             });
        }

        soap.createClient("wsdl_url", {
            disableCache: true,
            envelopeKey: "env"
        }, Call, "endpoint_url");

    });
};

getTokens("service").then((Auth) => {
    console.log(Auth);
});

Also note that:

  • I follow your variable names, but identifiers with Capital letter at the beginning should be used for constructors or class names.
  • Also check your TypeScript/JavaScript/EcmaScript version to see if arrow functions (like in my example) are compatible. If not you can convert then to normal anonymous functions.
Daniel Duarte
  • 464
  • 4
  • 12
  • Thanks Daniel, yes i can get the value inside the callback function "Call". But i need the value outside of "Call" as it can return it from the container function "getTokens". – Gustavo Sep 20 '19 at 00:50
  • Just added an example modifying your code. Hope that helps. Also, if you can use asyn/await syntax in your code, you can change the last part to: `const Auth = await getTokens("service");`. That would wait for the result and leave it in `Auth`, but it is equivalent, just syntax-sugar. – Daniel Duarte Sep 20 '19 at 00:52
  • Thank Daniel, I try with promises an so on, but I think some buggy behavior whit promises inside promises. The code is in TypeScript. I not discard the buggy is my brain :-) last await dont work – Gustavo Sep 20 '19 at 00:57
  • Oh, one more comment about the `await`. They must be inside an `async` function. Forgot to tell you in my prev answer. You can just create an async function as a IIFE. – Daniel Duarte Sep 20 '19 at 01:01