Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

vue/js 关于在封装tryCatch里面执行异步接口的问题

tryCatch(method) {
      return async (...args) => {
        try {
          console.log('进') //正常情况下没进到这一步
          return await method(...args);
        } catch (error) {
          console.log('error',error) //出错了也没到这一步
          return
        }
      }
    }
this.tryCatch(editUser(data)) //editUser(data) 是请求接口方法 
//没封装的写法是 
//async complex(data){
//  try {
//        await editUser(data)
//      } catch (error) {
//        return
//      }
//}

以上是封装的tryCatch代码,执行this.tryCatch(editUser(data))后有请求接口,但是没有进入到try里,出错了也没执行到catch里,请教下大家是哪里出错了,该怎么改

在这里先谢谢各位大神们的解答了,小弟感激不尽!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你这里有柯里化的意味了,试试这样的写法

this.tryCatch(editUser)(data)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...