2
votes

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. 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. 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:

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. So I am sure that the callback function inside promise is not executed. But not sure how to fix it.

Any feedback is appreciated! Thanks!

1
you fixed the missing return. you should edit it then. so your problem changes you receive undefined (logger.Info(hostIps); //hostIps is undefine) for the argument. something is with findHostIps(params, function(err, data) { wrong. can you provide that codedi3
you possibly are doing something wrong™ in findHostIps - but you haven't shown that function, so, I'm guessing™Jaromanda X
Perhaps my comment was vague ... what is the code for findHostIps - please share it in your question, it's clearly not calling the callback for some reasonJaromanda X
Where is the value of dns in getHostIps(dns, hostZoneId) coming from? (The dns parameter of getHostedZoneId(dns) is not in scope)traktor
Hi All, the problem is solved. I am writing my code based on previous shitty code which is written by previous eng in the team. After I delete his code, everything works fine. I think there might be some race condition issue in the js file...Kelsey Zhou

1 Answers

1
votes

You have to return the promise from your then statement to complete the chain.

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