【问题标题】:How to resolve promise once we get response from api call? [duplicate]一旦我们得到 api 调用的响应,如何解决 promise? [复制]
【发布时间】:2020-02-09 01:09:26
【问题描述】:

我有在页面加载时调用的方法 dropDowndata 并且它正在呈现 dropdownbData 所以在 getClientList 我得到了来自 api 调用的响应但是当我试图在 dropdDownData 中调用 getCLientList 时它显示 Promise 待处理这导致未定义的data。下面的代码中实现了什么错误?如何解决 axios 承诺?

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

【问题讨论】:

标签: javascript jquery promise axios


【解决方案1】:

您将 Promise 分配给变量 data,而不是其结果。您可以使用 async/await 来等待解决,或者在 .then 回调中处理事情。

async function dropdDowndata() {
        var _env = getEnv();
        var data = await getClientList();
        // Do processing of data here...
};

【讨论】:

  • 谢谢你,我正在寻找这个解决方案!
猜你喜欢
  • 2015-06-01
  • 2015-12-27
  • 1970-01-01
  • 2016-06-29
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
相关资源
最近更新 更多