1

I couldn't get my for loop to run synchronously.The order of registration that it returns for each domain changes. I guess whichever answers the fastest brings those records first. Where am I doing wrong?

const rrtypes= ["A","MX","CNAME","NS","TXT"];
var resData = [];
    
export const getAllRecords = async (req,res) => {
    const {domain} = req.params;
         for await(const rrtype of rrtypes){
             dns.resolve (domain, rrtype, (err, records) => { 
             resData.push(rrtype+" "+records);
        });
    }    
        res.send(resData);
        resData = [];      
}
aliveli49
  • 31
  • 4
  • 1
    Don't use `async`/`await` if you want to run it synchronously? And [never use `for await … of` on an array](https://stackoverflow.com/q/60706179/1048572)! – Bergi Aug 24 '21 at 23:25
  • 1
    Did you mean you want to run the loop *sequentially*? Then you need to [promisify](https://stackoverflow.com/q/22519784/1048572) the `dns.resolve` call, and `await` *that*. – Bergi Aug 24 '21 at 23:27
  • 1
    Also move the `var resData = [];` inside your function declaration, or else the array would be shared between multiple calls. – Bergi Aug 24 '21 at 23:28

2 Answers2

1

The problem lies in your use of await. await should be used when calling async functions. At the moment you are using it when instantiating variables from an array.

Thanks to a comment by slebetman I was able to see this dns.resolve does not return a promise but uses callbacks to manage the async calls. As he also suggested we can fix this by creating a promise to manage these callbacks.

const rrtypes= ["A","MX","CNAME","NS","TXT"];
var resData = [];

const dnsResolvePromise = (domain, rrtype) => {
  return new Promise((resolve, reject) => {
    dns.resolve(domain, rrtype, (err, records) => { 
        if(err) return reject(err);
        resolve(rrtype+" "+records);
    });
  })
}

export const getAllRecords = async (req,res) => {
    const {domain} = req.params;
    for(const rrtype of rrtypes){
        try{
            const records = await dnsResolvePromise(domain, rrtype);
            resData.push(records);
       } catch (e){
         // Handle error
       }
    }    
    res.send(resData);
    resData = [];      
}

Hans Krohn
  • 421
  • 2
  • 7
  • 1
    It's a standard node.js function: https://nodejs.org/api/dns.html#dns_dns_resolve_hostname_rrtype_callback. And NO, it DOES NOT return a promise. You will have to show him how to wrap callbacks in promises – slebetman Aug 25 '21 at 03:52
  • 1
    @slebetman Ah thanks for pointing this out to me, I did not know this before. – Hans Krohn Aug 25 '21 at 04:12
  • 1
    or you can use the already built in `dns.promises.resolve` which uses promise and not callback so you do not need a helper functionn to convert callbacks to promises – tam.teixeira Aug 25 '21 at 22:57
  • 1
    This will keep them in order but it will also be slow as it waits for each promise before running the next – Dominic Aug 25 '21 at 23:33
  • 1
    @Dominic, that is true, alternatively `Promise.all` make them in parallel – tam.teixeira Aug 25 '21 at 23:36
1

Notice that you can use the dns.promises.resolve function: https://nodejs.org/api/dns.html#dns_dnspromises_resolve_hostname_rrtype

so you would change your code to:

const rrtypes = ["A", "MX", "CNAME", "NS", "TXT"];

export const getAllRecords = async (req, res) => {
  const { domain } = req.params;

  // Notice that resData should be local to this function
  let resData = [];
  for (const rrtype of rrtypes) {
    try {
      const records = await dns.promises.resolve(domain, rrtype);
      resData.push(rrtype + " " + records);
      // IMO this would be better: resData.push({ type: rrtype, value: records });
    } catch (err) {
      // Log the error here
    }
  }

  res.send(resData);
};
tam.teixeira
  • 805
  • 7
  • 10