javascript 使用try catch块将.then .catch转换为异步等待[已关闭]

q0qdq0h2  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(131)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
昨天关门了。
Improve this question
我正在尝试将我的axios请求转换为try catch块,有人能帮忙吗?
我的代码:

const cancelSub = (subscriptionId) => {
    axios
      .post("http://localhost:4242/cancelSub", { subscriptionId })
      .then((res) => {
        console.log(res);
      })
      .catch((err) => {
        toast.success("this worked");
        console.log(err);
      });
  };
xxls0lw8

xxls0lw81#

const cancelSub = async (subscriptionId) => {
    try{
        const res = await axios.post("http://localhost:4242/cancelSub",{subscriptionId})
        console.log(res);
    }
    catch(err){
        toast.error("Something went wrong!");
        console.log(err);
    }
}

相关问题