mardi 28 juillet 2020

How to handling error in decorate function?

I'm trying to decorate my function that has an error handling like this.

// Target function 

async function update(){
  try{
   await someAsyncFunction()
  }
  catch(err){
   return Promise.reject(err)
  }
}

// Decorate function 

async function notify(fn){
 try{
  await fn()
  await publish()
 }
 catch(err){
  return err
 }

So, we can call the function like this

notify(update)

But the problem comes when the publish method throwing an error, it should break the function of notifying. The goal of this method the notify should be logging error only and if the update method throwing an error, it should fail to publish.

Thank you.

Aucun commentaire:

Enregistrer un commentaire