简体   繁体   中英

Promise { <pending> } - for last async function

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.createReturnLabel from 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())

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM