I've tried to reduce my "problem" to be more concise. I'm trying to get back from an async function (the fetch must be in a specific function cause I'm doing other call in there).
testAsync is fired when a button is clicked :
async function testAsync(){
console.log("start async");
let result = await go(); // Waiting promise
console.log("result "+result);
alert(result);
}
Then I want to do the "go" thing
async function go(){
fetch('https://apiurl.com', {
method: 'POST',
body: '{"URL":"anotherurltotest.com"}'
})
.then(response => response.json())
.then(json => {return json;})
.catch(err => {
console.log(err)
})
}
With this method : result is undefined and fired immediately, not awaiting the return json from then/fetch.
But I've tried this :
async function go(){
return await fetch('https://apiurl.com', {
method: 'POST',
body: '{"URL":"anotherurltotest.com"}'
})
.then(response => response.json())
.catch(err => {
console.log(err)
})
}
With this, the result are fired only after a short time and are populated.
Why is the return in the then not working ? but return awaiting all the fetch, yes ?
Thanks :)