Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Run The Build Synchronously

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 = [];      
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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);
};
about 4 years ago · Juan Pablo Isaza Report

0

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 = [];      
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!