Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
How to handle errors with Async and Await [Node.js]

I have already a function written with bluebird promises and I would like to rewrite it with async and await. When I have made the changes I have found out that earlier with promises the reject statement always transfers the control to called function catch block though if the catch block is already there in the file from where we are rejecting. How to handle this situation properly with async and await?. (Added comments to the code to explain the issue)

With Promise:

const callingFunc = (req, res) => {

    return new Promise((resolve, reject) => {
        // execute request which returns promise
        functionCall()
            .then((response) => {
                let error;

                try {
                    xml2js(response.body, { explicitArray: false }, (err, result) => {
                        if (err) {
                            return reject(err); /* throws the correct error to catch block of the file from where callingFunc is called*/ 
                        }

                        if (!_.isEmpty(result.Response.errorCode)) {
                            return reject(result.Response); /* throws the correct error to the catch block of the file from where callingFunc is called*/ 
                        }

                        return resolve(result);
                    });
                } catch (e) {
                    error = new Error('xml2js conversion error');
                    reject(error);
                }
            })
            .catch((error) => {
                const Error = new Error('Internal Server Error');
                reject(Error);
            });
    });
};

With async and await:

const callingFunc = (req, res) => {

    try {
        const response = await functionCall();
        let error;

        try {
            xml2js(response.body, { explicitArray: false }, (err, result) => {
                if (err) {
                    throw (err); /* throws the error to the below catch block and returning xml2js conversion error and changing behaviour*/ 
                }

                if (!_.isEmpty(result.Response.errorCode)) {
                    throw result.Response; /* throws the error to the below catch block and returning xml2js conversion error and changing behaviour*/
                }

                return result;
            });
        } catch (e) {
            error = new Error('xml2js conversion error');
            throw error;
        }
    } catch(error) {
        const Error = new Error('Internal Server Error');
        throw Error;
    }
}; 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If functionCall returns a promise, then this code is inappropriate...

return new Promise((resolve, reject) => {
    // execute request which returns promise
    functionCall()
        .then((response) => {

If xml2js is async using callbacks, then it is appropriate to wrap it in a promise...

// return a promise that resolves with the result of xml2js
async function xml2js_promise(body) {
  return new Promise((resolve, reject) => {
    xml2js(body, { explicitArray: false }, (err, result) => {
      if (err) reject(err);
      else if (!_.isEmpty(result.Response.errorCode)) reject(result.Response);
      else resolve(result);
    });
  });
}

Now we can await these. There's no need to nest the try's. (And you only need the try if you're going to do something on the catch).

async callingFunction = (req, res) => {
    try {
      const response = await functionCall();
    } catch (error) {
      // do something with this error
    }
    try {
      const result = await xml2js_promise(response.body)
    } catch(error) {
      // do something with this error
    }
    return result;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda