javascript - How can I make this more DRY -


new javascript , need in making more dry. not sure if using switch statement way go...

.catch((err) => {   console.log(err)   let emaildup = 'user exists'   let userdup = 'username exists'   if (err.response.body.includes(emaildup)) {     this.setstate({       errmessage: erremaildup,       error: true,       loading: false     })   } else if (err.response.body.includes(userdup)) {     this.setstate({       errmessage: erruserdup,       error: true,       loading: false     })   } else {     this.setstate({       errmessage: errdefault,       error: true,       loading: false     })   } } 

the following code easy read , maintain (adding more cases easy well):

.catch((err) => {     let emaildup = 'user exists';     let userdup = 'username exists';     let errmessage;      if (err.response.body.includes(emaildup)) {         errmessage = erremaildup;      } else if (err.response.body.includes(userdup)) {         errmessage = erruserdup;      } else {         errmessage = errdefault;     }      this.setstate({         errmessage: errmessage,         error: true,         loading: false     }); } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -