0

i have created below code to get some data, i am returning that data in a function and i am calling that function in a variable like below :

 let tmsValue = await GetTMS(fymcl,this.model); 
    console.log("tmsValue",tmsValue);

   let tms;
   var GetTMS = async function getTMS(yearMetricCombo, modelData) : Promise<any>{
     return new Promise((resolve, reject)=>{
     modelData
       .query("11111")
       .where("fymcl").equals(yearMetricCombo) // tm|FY18|Promtions
       .loadAll()
       .exec((error, data) => {
         if (error) {
           reject(error);
         } else {
           tms = String(data.Items[0].attrs.uploadtms);
           resolve(tms);
           // console.log("tms",tms); // iam getting value over here
         }
       });
       return tms;
     });
   }

The issue is let tmsValue = await GetTMS(fymcl,this.model); // this line is saying 'await' expression is only allowed within an async function.

I am not sure what i need to do.

6
  • 1
    Your function GetTMS has no return statement, so the function implicitly returns undefined. It looks like it's making an asynchronous db call, so you may want to look into using a Promise here. Take a look at this question Commented Apr 13, 2018 at 18:23
  • i have updated with a promise Commented Apr 13, 2018 at 19:53
  • But now its saying await can only be used inside async function Commented Apr 13, 2018 at 19:55
  • 1
    Indeed, the await keyword can only be used in a function declared with the async keyword. That means it cannot appear at the root level of your file. If you need to use the getTMS function at the root of the file, you'll have to put the code that uses its result inside the .then callback of the promise. If you need to use getTMS in another function, you either need to use the .then method as mentioned, or mark the function as async. Commented Apr 13, 2018 at 20:01
  • i have marked it as async, can u please make some update in my code to include .then() Commented Apr 13, 2018 at 21:05

1 Answer 1

1

The function doesn't return:

 function GetTMS(yearMetricCombo, modelData){
      // NO RETURN STATEMENT HERE
      modelData
      .query("11111")
      .where("fymcl").equals(yearMetricCombo) // tm|FY18|Promtions
      .loadAll()
      .exec((error, data) => {
        console.log("error",error);
        if (error) {
        } else {
          let tms = String(data.Items[0].attrs.uploadtms);
          console.log("tms",tms);
          // this returns from the *arrow function*, not
          // the enclosing function declaration
          return tms;

        }
      });
      // NO RETURN STATEMENT HERE
    }

See the question linked to from @CRice's comment for how to return from an async call

Sign up to request clarification or add additional context in comments.

4 Comments

why i need a return statement at the top ?
You don't, I'm just listing the two places where a return statement could be but isn't
This doesn't have anything to do with the OP's error they asked about. The OP's error is because they're trying to use await outside of an async defined function which you cannot do. That's a core requirement of using await.
OP has been substantially edited since I posted this :\

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.