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())