0

I have method dropDowndata that is being called on page load and it is rendering dropdownbData so in getClientList i got the response from api call however when i am trying to call getCLientListin dropdDownData it shows Promise is pending and that result in undefined data. what is implemented wrong in below code ? How to resolve axios promise ?

main.js

function dropdDowndata() {
        var _env = getEnv();
        var data;
        getClientList().then(function(response) {
          data = response;
        });

};

function getClientList = function() {
    debugger;
    var resetResponse = "";
    var refId = GUIDUtil.Guid.newGuid().toString("N");
    var details = {};
    var params = {
        "Request": {
            "header": {
                "serviceContext": {
                    "apiVersion": "1.0",
                    "lineOfBusiness": "SPD"
                },
                "securityContext": {
                    "securityType": "apiKey",
                    "apiKey": "26283629239362127"
                }
            },
            "details": details
        }
    };
    var clientListParams = {
        url: getHostName() + '/test/client',
        data: params,
        method: 'POST',

        headers: {
            'Content-Type': 'application/json'
        }
    };

    return axios(clientListParams).then(function(response) {
        return response.data;

    }).catch(error);
};
aftab
  • 693
  • 1
  • 12
  • 27
  • [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?rq=1) – Andreas Oct 11 '19 at 13:36
  • What's `function getClientList = function() {` ?? It should be `var getClientList = function() {` i dont see anything wrong in code other than this – vipul patel Oct 11 '19 at 13:49

1 Answers1

1

You're assigning the Promise to the variable data, not its result. You can either use async/await to wait for the resolution, or handle things inside the .then callback.

async function dropdDowndata() {
        var _env = getEnv();
        var data = await getClientList();
        // Do processing of data here...
};
IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26