I tried various ways mentioned on stackoverflow from similar questions but still unable to resolve the issue. I have a plain function (not a async function) which contains various switch cases. I have a separate function which either resolves or rejects. Eg-
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;
}
}
}
I cannot make this function for some reason, is there any way I can return feesPaid value? Seems it doesn't update anything or any checks. feesPaid value is necessary, as it's used for further calculation. Please help.
There are a few ways to fix this:
async function
.No matter what you do, it will always affect other code blocks.
Using .then()
won't work because the function will return long before the Promise resolves. Returning just feesPaid
won't work because the Promise hasn't finished before feesPaid
is assigned a value.
The easiest solution to your problem is to just modify your function to utilize async/await
, and modify all effected code to utilize async/await
as well.