Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda