0

I have a module for nodejs where i search mx records of email service, then i call a reduce to find the one with lowest priority (i will connect to this mx record to send a message) and then i need to save this value (exactly smtp.gmail.com) and pass it into my another module. What do i do wrong ?

const dns = require('dns');

async function getMX() {
return new Promise(function (resolve, reject) {
    dns.resolveMx('gmail.com', function (err, addresses) {
        if (err) {
            resolve(null);
        }
        else {
            resolve(addresses);
        }
    });
}).then(listOfMX => { // here i'm looking for value with lowest priority
    return arr = listOfMX.reduce((previous, current) => {
        if (previous.priority < current.priority) {
            return previous
        }
        return current;
    })
}).then(response => {
    console.log(response[0]);
    return response.exchange; // i get {exchange:"smtp.aspko.com", priority: 5}, and from this i return only the exchange
}).catch(err => {
    console.log(err);
});
};
async function f() { // async function to solve the Promise <pending>
   const response = await getMX();
   console.log(response)
};

f(); // here i call the main function

Anyway i still see or Promise or undefined and i can't just add a return in async function f(), which will solve my problem (when i will call f() it should return me the one value, but instead i see undefined)

Cribe
  • 1
  • 1
  • 4
  • 1
    _"call f() it should return me the one value"_ - no, it will always return a promise. If you want the value you must use `.then` or `await` – evolutionxbox Aug 10 '22 at 11:59
  • 1
    Does this answer your question? [How to access the value of a promise?](https://stackoverflow.com/questions/29516390/how-to-access-the-value-of-a-promise) – evolutionxbox Aug 10 '22 at 12:00
  • unfortunately it doesn't answer my question, i still have Promise pending – Cribe Aug 10 '22 at 12:40
  • How are you trying to use the value returned from `f`? – evolutionxbox Aug 10 '22 at 12:40
  • i'm importing module with function f() in my main file, then i make net.createConnection(port, host), where port is 25, host is result of function f() – Cribe Aug 10 '22 at 12:54

0 Answers0