0

I have two main functions. The first one gets the SOAP data from an API. The second one parses it to json and then formats it into the object I need to store. I have been logging and seeing the return values, and as of so far until now, it's been fine. However, I am calling exports.createReturnLabelfrom my test file and all I can see is Promise { <pending> } . I am returning a value at the last function. Does this code look weird to your? How can I clean it up?

const soapRequest = require('easy-soap-request');
const xmlParser = require('xml2json')

exports.createReturnLabel = async () => {
    const xml = hidden

    const url = 'https://ws.paketomat.at/service-1.0.4.php';

    const sampleHeaders = {
        'Content-Type': 'text/xml;charset=UTF-8',
    };

    const auth = async () => {
        const {
            response
        } = await soapRequest({
            url: url,
            headers: sampleHeaders,
            xml: xml,
            timeout: 2000
        });
        const {
            body,
            statusCode
        } = response;

        return body
    }

    const fetchLabel = async () => {
        const soapData = await auth();
        try {
            const json = xmlParser.toJson(soapData)
            const labelData = JSON.parse(json)["SOAP-ENV:Envelope"]["SOAP-ENV:Body"]["ns1:getLabelResponse"]
            return {
                courier: 'dpd',
                tracking_number: labelData["return"]["paknr"],
                barCode: labelData["return"]["barcodecontent"],
                printLabel: labelData["return"]["label"],
                _ref: null
            }
        } catch (e) {
            return (e)
        }
    }

    return fetchLabel()
}

calling from my test file return console.log(file.createReturnLabel())

zhulien
  • 5,145
  • 3
  • 22
  • 36
Benjamints
  • 849
  • 1
  • 10
  • 24
  • should be: `return console.log(await file.createReturnLabel())` – Liam Mar 15 '21 at 11:05
  • Well, you return a promise instead of a result. You should `await` it in your test. – zhulien Mar 15 '21 at 11:05
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Liam Mar 15 '21 at 11:06

1 Answers1

0

There's an async function call inside your code.

Should be: return await fetchLabel(), so that it awaits for completion before going on its merry way.

Nicholas
  • 2,800
  • 1
  • 14
  • 21