Javascript 单个异步函数中的链式等待 异步函数提交表单(e){ e、 预防默认值() console.log(例如target) 等待axios.post('/api/',{username,password}) 。然后(res=>{ console.log(res.data) const token=res.data.token 如果(令牌){ const json=jwt.decode(token)为{[key:string]:string} 等待axios.post('/api/',{token}) 。然后(res=>{ 常数数据=分辨率数据

Javascript 单个异步函数中的链式等待 异步函数提交表单(e){ e、 预防默认值() console.log(例如target) 等待axios.post('/api/',{username,password}) 。然后(res=>{ console.log(res.data) const token=res.data.token 如果(令牌){ const json=jwt.decode(token)为{[key:string]:string} 等待axios.post('/api/',{token}) 。然后(res=>{ 常数数据=分辨率数据,javascript,asynchronous,async-await,axios,next.js,Javascript,Asynchronous,Async Await,Axios,Next.js,问题就在这条线上 等待axios.post('/api/',{token}) 我的IDE声明 TS1308:“await”表达式只允许在异步函数内和模块的顶层使用 它目前无法编译,我正在尝试将令牌值传递到另一个axios API调用中。但是,由于上述错误,这目前不可能实现。任何有关如何修复此问题的线索都将受到欢迎。wait必须直接在异步函数中,您需要将回调函数设置为异步嗯 异步函数提交表单(e){ e、 预防默认值() console.log(例如target) 等待axios.post('/a

问题就在这条线上

等待axios.post('/api/',{token})

我的IDE声明

TS1308:“await”表达式只允许在异步函数内和模块的顶层使用


它目前无法编译,我正在尝试将令牌值传递到另一个axios API调用中。但是,由于上述错误,这目前不可能实现。任何有关如何修复此问题的线索都将受到欢迎。

wait
必须直接在
异步
函数中,您需要将回调函数设置为
异步

异步函数提交表单(e){
e、 预防默认值()
console.log(例如target)
等待axios.post('/api/',{username,password})
//这里异步
。然后(异步res=>{
console.log(res.data)
const token=res.data.token
如果(令牌){
const json=jwt.decode(token)为{[key:string]:string}
等待axios.post('/api/',{token})
。然后(res=>{
常数数据=分辨率数据
正如Bergi所提到的,没有必要将
wait
混合使用。然后,您的代码可以是这样的:

异步函数提交表单(e){
e、 预防默认值()
console.log(例如target)
let res=wait axios.post('/api/',{username,password})
console.log(res.data)
const token=res.data.token
如果(令牌){
const json=jwt.decode(token)为{[key:string]:string}
res=wait axios.post('/api/',{token})
常数数据=分辨率数据;

等待
必须直接在
异步
函数中,您还需要使回调函数
异步

异步函数提交表单(e){
e、 预防默认值()
console.log(例如target)
等待axios.post('/api/',{username,password})
//这里异步
。然后(异步res=>{
console.log(res.data)
const token=res.data.token
如果(令牌){
const json=jwt.decode(token)为{[key:string]:string}
等待axios.post('/api/',{token})
。然后(res=>{
常数数据=分辨率数据
正如Bergi所提到的,没有必要将
wait
混合使用。然后,您的代码可以是这样的:

异步函数提交表单(e){
e、 预防默认值()
console.log(例如target)
let res=wait axios.post('/api/',{username,password})
console.log(res.data)
const token=res.data.token
如果(令牌){
const json=jwt.decode(token)为{[key:string]:string}
res=wait axios.post('/api/',{token})
常数数据=分辨率数据;

then()的函数回调不是异步的,请不要将
wait
then
语法混合使用。Write
const res=wait axios.post('/api/',…);
改为对
then()的函数回调
不是异步的,不要将
await
then
语法混合使用。Write
const res=await axios.post('/api/',…);
而不是。Op为什么需要then()首先?@charlietfl我试图从语法上解释
async
是如何工作的。对于逻辑,我添加了信息after@hackape否决票是在第二段加入之前投下的。好吧,即使没有第二段,这也解释了为什么OP的案例没有汇编,这正是他所问的!去掉不必要的then callback很好添加,但是没有这一部分,答案仍然是正确的。感谢您的快速响应,这是我糟糕的组合等待&。那么Op为什么需要一个then()首先?@charlietfl我试图从语法上解释
async
是如何工作的。对于逻辑,我添加了信息after@hackape否决票是在第二段加入之前投下的。好吧,即使没有第二段,这也解释了为什么OP的案例没有汇编,这正是他所问的!去掉不必要的then callback很好添加,但是没有这一部分,答案仍然是正确的。感谢您的快速响应,这是我的糟糕组合等待&
async function submitForm(e){
  e.preventDefault()
  console.log(e.target)

  await axios.post('/api/<PATH>', {username, password})
    .then(res => {
      console.log(res.data)
      const token = res.data.token
      if (token){
        const json = jwt.decode(token) as { [key: string]: string}
  
        await axios.post('/api/<PATH>', { token })
          .then(res => {
            const data = res.data