Get the name of a promise - async

1

I understand that if you have a function you can get the name with the property name and if it is a class you can get the name prototype.name but as I get the name of a promise, the problem is as follows I have several functions type async and when they have an error I skip the event unhandledRejection which returns me two parameters error and promise now what I want is to print the name of the function that returns the error an example of my code

app.get('/', async function index (req, res, next) {
  throw new Error('hay ja jay, un error compadre')
}) 
process.on('unhandledRejection', (err, p) => {
    console.error('a ocurrido en la funcion ${p} el siguiente error: ', err)    
})

lv5 summary: how do I get the name of p

edit:

the code that comes from an api json made in express and I have different routes each controller of a route uses a function callback that can be a normal function anonymous, a promise or a function async , this last type of function I use it for reading reasons in the code and it actually behaves as if it were a promise.

Now it is good practice for debugging in nodejs to have the functions named so I have the function as follows

async function index (req, res, next) {

}

these functions are captured errors with try and catch as any and being a callback of an express endpoint receives the parameters req , res and next the normal thing is to pass the error to next for it to handle the express handler error but what I want is to make the error handler of the own node unhandledRejection and see if I can register with the name of the promise of this case bone 'index'

    
asked by soldat25 12.12.2018 в 14:29
source

1 answer

2

Reading the documentation of throwing exceptions from Mozilla you can do the following:

  • See the function that I throw at you the error
  • Show the trace from where it failed
  • Show 'hay ja jay, un error compadre'

A practical example would be below:

const miPromesa = () => {
  return new Promise((resolve, reject) => {
      throw new Error('hay ja jay, un error compadre');
  });
};

const main = async () => {
  try {
    await miPromesa();
  } catch (ex) {
    console.error('Chispas!! me han retornado un ${ex.name}  su mensaje es "${ex.message}" ')
    console.info('la traza del error fue : ${ex.stack}')

  }
};

main();

Assuming that your code is correct, it would be as follows:

app.get('/', async function index (req, res, next) {
  Promise.reject(new Error('hay ja jay, un error compadre'))
}) 
process.on('unhandledRejection', ( p, err) => {
    console.error('Chispas!! me han retornado un ${err.name}  su mensaje es "${err.message}" ') 
console.info('la traza del error fue : ${err.stack}')
})

Now the must be for error handling of a Promise is using resolve and reject the code should be:

    const miPromesa = () => {
      return new Promise((resolve, reject) => {
        reject('hay ja jay, un error compadre en linea 2 al retorna nueva promesa verifique parametros' );
      });
    };

    const main = async () => {
      try {
        await miPromesa();
      } catch (ex) {
        console.error('Chispas!! me han retornado un Error ${ex}')


      }
    };

    main();
    
answered by 12.12.2018 в 15:16