- 使用可选链代替函数兼容判断
// bad
if (func) func()
func() && func()
// good
func?.()
- Promise
fulfilled
和rejected
都需要处理的逻辑超过一条,统一放到finally
// bad
promise
.then((res) => {
setLoading(false)
closeModal()
// ...
})
.catch((error) => {
setLoading(false)
closeModal()
})
// good
promise
.then((res) => {})
.catch((error) => {})
.finally(() => {
setLoading(false)
closeModal()
})