There is a loadJson function that returns the Json of a firebase link
JavaScript
x
6
1
async function loadJson(url) {
2
let response = await fetch(url)
3
let data = await response.json()
4
return data
5
}
6
I am trying to assign the value of loadJson()
to this variable and use it in a promise.
JavaScript
1
7
1
let indexJSON = await loadJson(url)
2
3
indexJSON.then(() => {
4
// some code
5
})
6
7
But why does this code throws the following error?
JavaScript
1
2
1
Uncaught SyntaxError: await is only valid in async function
2
Advertisement
Answer
your problem is your await
here:
JavaScript
1
6
1
let indexJSON = await loadJson(url)
2
3
indexJSON.then(() => {
4
// some code
5
})
6
if you want the promise call the function without await
:
JavaScript
1
3
1
let indexJSON = loadJson(url)
2
indexJSON.then( )
3