campcc / coding-best-practice

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

coding-best-practice

  • 使用可选链代替函数兼容判断
// bad
if (func) func()
func() && func()

// good
func?.()
  • Promise fulfilledrejected 都需要处理的逻辑超过一条,统一放到 finally
// bad
promise
  .then((res) => {
    setLoading(false)
    closeModal()
    // ...
  })
  .catch((error) => {
    setLoading(false)
    closeModal()
  })

// good
promise
  .then((res) => {})
  .catch((error) => {})
  .finally(() => {
    setLoading(false)
    closeModal()
  })

About