var res;
const fetchques = async () => {
await fetch('url')
.then(function(response) {
response.json().then(function(r) {
res = r["results"]
console.log(res) //let line 1
})
}).catch((err) => {
console.log(err)
})
}
const getques = async () => {
await fetchques()
console.log(res) // let line 2
}
//getques() is called after body loads, ie <body onload="getques()">
In my output, it is printing line 2 first then line 1. Isnt await supposed to wait for the called function to finish? What am I doing wrong?
I do not want to use promises yet.
I even tried await fetchques().then(function(el){console.log(res)}) for getques(), but in vain. Not even .then() waited.
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment