0

is it possible to use async await on class functions ? i have read some where we can use it by adding static keyword to the function but looks like it doesn't work.

static async getDetails() { }

class Auto {
    async getDetails() {
        try{
                const res = await fetch('https://jsonplaceholder.typicode.com/posts/1')
                const data = await res.json()
                console.log(data)
            }catch(err){
                console.log('error from fetch : ',err)
         }
    }
}

const auto = new Auto();
auto.getDetails();

the above code is working but the actual problem is when i return result its returning promise.

    class Auto {
        async getDetails() {
            try{
                    const res = await fetch('https://jsonplaceholder.typicode.com/posts/1')
                    const data = await res.json()
                    return data.title;
                }catch(err){
                    console.log('error from fetch : ',err)
             }
        }
    }

const auto = new Auto();
const getTheFinalResult = () =>{
        let res = ''
        auto.getDetails().then(promStr => res = promStr)
        return res;
    }
console.log(getTheFinalResult())
sravan ganji
  • 4,774
  • 3
  • 25
  • 37
  • Have you tested it or looked up the documentation on `async`/`await`? – zzzzBov May 09 '18 at 02:04
  • 1
    Your code works, what error are you getting?. Have in mind that you have to use `node >= 7.6` in order to use `async/await` – Marcos Casagrande May 09 '18 at 02:09
  • `is it possible to use async await on class functions`: Yes. `doesn't work`: You're doing something wrong but the code posted doesn't show what. Since `getDetails` is missing a return statement it will return a promise resolving in undefined and never rejecting because you are catching any error and not returning anything in the catch either. – HMR May 09 '18 at 02:10
  • Why do you think that it doesn't work? How are you calling the static function? It should be called by class' name -- `Auto.getDetails()`. – 31piy May 09 '18 at 02:42
  • i am trying to use it in my nodejs app ,if i use function outside class its working fine , but its not working inside class – sravan ganji May 09 '18 at 02:47
  • Show us how you're using it because a class method is JUST a function so it works just fine with `async`, but I'm guessing that you aren't using it properly, but we'd have to see the code that uses it to help you with that. And, since you aren't returning anything from your `async` method either, it wouldn't be very useful. – jfriend00 May 09 '18 at 03:16
  • "*the actual problem is when i return result its returning promise*", yes, that's what `async` function do return. Always. Attached to a class proto or not. Whatever the content of your async function. – Kaiido May 09 '18 at 04:58
  • @Kaiido so what is the right way to get the resolved data – sravan ganji May 09 '18 at 05:00
  • `auto.getDetails().then(data=>{/*do something with the data*/})`. Or if you are calling it from an other async func, `const data = await auto.getDetails();` – Kaiido May 09 '18 at 05:01
  • yah i did try that its working fine , cant we do it again with another asyn/await because if i do that i have to change the code in lot of places in my app – sravan ganji May 09 '18 at 05:02
  • @Kaiido see my final code edit, i want to return `promStr` that i am getting from the `getDetails()` i am not understanding how to return the final value. really appreciate your help. – sravan ganji May 09 '18 at 05:44
  • @jfriend00@HMR@31piy @MarcosCasagrande @Kaiido please look at my second code snippet . there am trying to access `let res = ' '` inside `then` , i want to return the `promStr` for this `getTheFinalResult` function – sravan ganji May 09 '18 at 06:04
  • You can NEVER make an async result into a synchronous result. NEVER. `getDetails()` is asynchronous. It will return a promise and that promise will be resolved some time in the future, AFTER `getTheFinalResult()` has already returned. So, once the result is asynchronous somewhere, it will always be asynchronous. You can never return it directly from any caller. More explained here: [How do I return response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323). – jfriend00 May 09 '18 at 06:10
  • To say it another way, your `return res` will happen BEFORE `res = promStr` runs. That can never work. – jfriend00 May 09 '18 at 06:14
  • @jfriend00 i got it but how to solve the problem , i want to get the `promStr ` value when i call the `getTheFinalResult ` – sravan ganji May 09 '18 at 06:17
  • @SuperReact - You can't. `getTheFinalResult()` has to just return a promise too. You will ALWAYS use a promise value inside a `.then()` handler. That's async programming. No way around it. – jfriend00 May 09 '18 at 06:19

0 Answers0