【问题标题】:Await .then does not wait for the end before returningAwait .then 不等待结束才返回
【发布时间】:2019-06-24 09:49:35
【问题描述】:

async / await / .then 在继续之前不会等待函数结束。

async function getInfosDatastore(req, res, next) {
    var IP = req.body.IP;
    var username = req.body.username;
    var password = req.body.password;
    var token = '';
    var cluster = "";
    var policies = "";
    var datastores = "";
    url = 'https://simplivity@' + IP + '/api';
    await getInfos.getToken(url, username, password)
        .then(response => {
            token = response;
            getInfos.getDatastores(token, url)
                .then(response => {
                    datastores = response;
                });
            getInfos.getPolicies(token, url)
                .then(response => {
                    policies = response;
                });
            getInfos.getClusters(token, url)
                .then(response => {
                    cluster = response;
                });
        });
    res.json({ name: datastores, policy: policies, cluster: cluster });
}

输出是: - 令牌 - 测试 - res.json(但它是空的) - 每个函数中的console.log

应该是: - 令牌 - 每个函数中的console.log - 测试 - 具有正确值的 res.json

感谢您的帮助

【问题讨论】:

    标签: node.js request


    【解决方案1】:

    您没有将内部 Promise 与外部 Promise 链链接在一起。改为

    async function getInfosDatastore(req, res, next) {
        const { IP, username, password } = req.body;
        const url = 'https://simplivity@' + IP + '/api';
        const token = await getInfos.getToken(url, username, password);
        const [name, policy, cluster] = await Promise.all([
          getInfos.getDatastores(token, url),
          getInfos.getPolicies(token, url),
          getInfos.getClusters(token, url)
        ]);
        res.json({ name, policy, cluster });
    }
    

    使用await Promise.all,在解释器继续下一条语句之前,等待所有内部 Promise(并提取它们的解析值)。

    此外,请确保将 catch 放在 getInfosDatastore 的调用者上,以防其中一个 Promise 被拒绝(如果您还没有的话)。

    【讨论】:

    • 感谢您的回答,这有效。但我不明白为什么我的反应 JS 不打印它。我在数据中看到它,但在 data.datastores 中没有
    • 您返回的对象没有datastores 属性(不在您的原始函数中,也没有在这个函数中)。您的意思是使用.name 属性吗?
    • ``` this.setState({ namelist: data.datastores, clusterIDlist: data.cluster, policyIDlist: data.policy, submit: true, }) ``` 我这样做是为了回应取回值。我可能都颠倒了
    • 就像我说的,您返回的对象没有datastores 属性。请改用.name(或更改函数以返回具有name 属性的对象)
    【解决方案2】:

    没错,因为函数体中的以下3个回调函数是在下一个事件循环中(将来)执行的

        response => {
            token = response;
            getInfos.getDatastores(token, url)
                .then(response => {
                    datastores = response;
                });
            getInfos.getPolicies(token, url)
                .then(response => {
                    policies = response;
                });
            getInfos.getClusters(token, url)
                .then(response => {
                    cluster = response;
                });
        }
    

    您需要做的是删除then promise 方法,但使用await

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多