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.

user3205921
  • 51
  • 1
  • 8
  • 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](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – CRice Apr 13 '18 at 18:23
  • i have updated with a promise – user3205921 Apr 13 '18 at 19:53
  • But now its saying await can only be used inside async function – user3205921 Apr 13 '18 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`. – CRice Apr 13 '18 at 20:01
  • i have marked it as async, can u please make some update in my code to include .then() – user3205921 Apr 13 '18 at 21:05
  • You can only use `await` INSIDE a function declared with the `async` keyword. That's a core requirement of the language feature. Please go back and read the basics about `async` and `await`. There are hundreds of articles on this topic. You don't even show the containing function for your `await GetTMS()` so we can't advise any more specifically. – jfriend00 Apr 13 '18 at 21:05

1 Answers1

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

Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235