Throw error (Async/Await) in Meteor Method!

I have 2 methods:

// Async
Meteor.methods({
  async first(......){
      try {
      }catch(err){
         throw error Meteor.Error(......)
     }
  },
  second(){
      try {
            // Call the first method
            Meteor.call('first')
      }catch(err){
         throw error Meteor.Error(......)
     }
  }
})
-------------
// Client
Meteor.call('second', .......)

I don’t get throw error on second method, but the first method is error!!!
(UnhandledPromiseRejectionWarning: Unhandled promise rejection)
Please help me.

yes, because you are not awaiting the promise returned in second
Try:

Meteor.methods({
  async first(......){
      try {
      }catch(err){
         throw error Meteor.Error(......)
     }
  },
  async second(){
      try {
            // Call the first method
            await Meteor.call('first')
      }catch(err){
         throw error Meteor.Error(......)
     }
  }
})

Running an async function always returns a promise.
You can get the result of that promise with .then(func) or await

Any errors thrown in that async function causes the promise to reject
You can catch promise rejections with .catch(func) or try/catch if using await

2 Likes

@coagmano very thanks :blush: