开关盒内的Nodejs Promise给出错误

Nodejs Promise inside switch case giving error

我尝试了类似问题中Whosebug上提到的各种方法,但仍然无法解决问题。我有一个普通函数(不是异步函数),其中包含各种开关案例。我有一个单独的功能,可以解决或拒绝。例-

function Student (req) {
const id = req.id
const count = req.num
for (i=0;i<num,i++)
{
switch(id){
case 'a':
feesPaid=true
break;

case 'b':
const a = await getStudent (id) // gives error under getStudent that unexpected token eslint

getStudent (id).then((result)=>{
if(result.reciept){
feesPaid=true
}else{
feesPaid=false. // Returning pending promise which doesn't return anything, tried return feesPaid as well
}
}). catch (e)...
break;
}
}
}

出于某种原因我无法实现此功能,有什么方法可以 return feesPaid 值吗?似乎它没有更新任何东西或任何支票。 feesPaid 值是必需的,因为它用于进一步计算。请帮忙。

有几种方法可以解决此问题:

  1. 将函数更改为 async function
  2. 返回承诺。
  3. 使您的函数接受回调。

无论你做什么,都会影响其他代码块。

使用 .then() 将不起作用,因为该函数将 return 在 Promise 解析之前很久。只返回 feesPaid 是行不通的,因为 Promise 在 feesPaid 被赋值之前还没有完成。

解决您问题的最简单方法是修改您的函数以利用 async/await,并修改所有受影响的代码以也利用 async/await