admin管理员组

文章数量:1289364

I want to get MX records for hostname www.example. Node has got function for it.

dns.resolveMx(domain, callback)

But i don't want that callback thingy. I want something like sync call. e.g.

var records = dns.resolveMx(domain);

Is this possible ?

(Note:- I found that function in Node documentation. link:- .html)

I want to get MX records for hostname www.example.. Node has got function for it.

dns.resolveMx(domain, callback)

But i don't want that callback thingy. I want something like sync call. e.g.

var records = dns.resolveMx(domain);

Is this possible ?

(Note:- I found that function in Node documentation. link:- http://nodejs/api/dns.html)

Share Improve this question asked Sep 13, 2013 at 10:53 Umakant PatilUmakant Patil 2,3086 gold badges34 silver badges60 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Update 2021

There is now a promises submodule under dns module if some one is looking for sync calls.

Check more here

https://nodejs/api/dns.html#dns_class_dnspromises_resolver

const dnsPromises = require('dns').promises;
const demo1 = await dnsPromises.resolveMx("gmail.");

Is there any reason you want to block your application with a network operation? The DNS resolvers are called at the C level by the c-ares library which is asynchronous by design. Therefore, you can't make it synchronous. This is the code from the DNS module with the unneeded parts removed:

var cares = process.binding('cares_wrap');

function resolver(bindingName) {
  var binding = cares[bindingName];

  return function query(name, callback) {
    callback = makeAsync(callback);
    var req = {
      bindingName: bindingName,
      callback: callback,
      onplete: onresolve
    };
    var err = binding(req, name);
    if (err) throw errnoException(err, bindingName);
    callback.immediately = true;
    return req;
  }
}

var resolveMap = {};
exports.resolveMx = resolveMap.MX = resolver('queryMx');

本文标签: javascriptGet DNS MX records in NodeStack Overflow