简体   繁体   English

回调函数不在返回的 Promise 内执行

[英]Callback function is not executed within returned Promise

I have two functions that return promise.我有两个返回承诺的函数。 The first one provide host value, and the second one use the host value to get IP address.第一个提供主机值,第二个使用主机值来获取 IP 地址。 I can see that the first function is running without any issue.我可以看到第一个函数正在运行,没有任何问题。 But looks like the callback function side getHostIps is not executed at all.但看起来回调函数端getHostIps根本没有执行。 Not sure why it happens....what's wrong with my promise function?不知道为什么会发生......我的承诺功能有什么问题?

my promise chain:我的承诺链:

getHostedZoneId(dns)
.then(hostZoneId => {
   getHostIps(dns, hostZoneId);
})
.then(hostIps => {
    logger.Info(hostIps); //hostIps is undefined
})
.catch(err => logger.error(err));

getHostedZoneId:获取HostedZoneId:

var getHostedZoneId = function(dns) {
    var params = {
        DNSName: dns,
    };
    return new Promise((resolve, reject) => {
      findHostZoneByDNS(params, function(err, data) {
            if(err) {
                reject(err);
            }
            else {
                resolve(data);
            }
        });
    });
}

getHostIps:获取主机地址:

var getHostIps = function(dns, hostZoneId) {
    var params = {
        HostedZoneId: hostZoneId,
        StartRecordName: dns,
    };
    return new Promise((resolve, reject) => {
      findHostIps(params, function(err, data) {
           //logger.info("get there");
            if(err) {
                reject(err);
            }
            else {
                resolve(data);
            }
        });
    });
}

I logged hostIps and err and data, all of them are defined.我记录了 hostIps 和 err 以及数据,所有这些都已定义。 So I am sure that the callback function inside promise is not executed.所以我确定promise里面的回调函数没有被执行。 But not sure how to fix it.但不确定如何修复它。

Any feedback is appreciated!任何反馈表示赞赏! Thanks!谢谢!

You have to return the promise from your then statement to complete the chain.您必须从 then 语句中返回 promise 以完成链。

getHostedZoneId(dns)
.then(hostZoneId => {
   return getHostIps(dns, hostZoneId); // Add return
})
.then(hostIps => {
    logger.Info(hostIps);
})
.catch(err => logger.error(err));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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