I feel like I’m misunderstanding something fundamental about the JavaScript fetch API.
My ultimate goal here is to make a call to my PHP, get a response in JSON, and then do something in JavaScript when that response comes back. But I can’t get even a very stripped-down version to behave as I would expect. I’ve been looking at tutorials for hours, but I seem to be missing something crucial.
Here’s my index.php file, which generates two buttons:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<button class="btn">Test Button 1</button>
<button class="btn">Test Button 2</button>
<script src="test.js"></script>
</body>
</html>
This is the test.js file:
const btns = document.querySelectorAll('.btn');
function btnFunc() {
output = asyncFunc()
.then(response => console.log(response));
console.log(output);
}
async function asyncFunc() {
const response = await fetch('/target.php');
return response.json();
}
btns.forEach( btn => btn.addEventListener('click', btnFunc) );
And finally, this is target.php, which just sends back some JSON:
<?php
header('Content-type: application/json');
echo json_encode(['foo' => 1, 'bar' => 2]);
What I would expect to be logged to the console is two instances of my JSON data. Instead, I’m getting this:
Promise { <state>: "pending" }
Object { foo: 1, bar: 2 }
Why is the promise still pending when I’ve returned a response to it from asyncFunc
? How can I get the JSON back out of the fetch request so I can work with it? I’ve been looking at tutorials, but they all seem to stop with logging it to the console inside the fetch function, and I feel like it’s the next step I’m missing.