I am in the process of updating my code from using callbacks to utilize async
and await
. However, I am a bit confused on how to properly implement it within my current setup.
The current setup involves:
- Clicking a button that triggers the
clicker
function - The
clicker
function then triggers theajax
function - The
ajax
function performs afetch
request - After receiving a response, I need to pass it back to the
clicker
function
Currently, I am receiving an undefined
value from the ajax
function. Can someone please assist me in identifying the issue?
var ajax = function() {
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => { console.log('ajax function', json); return json; })
.catch(error => console.log(error))
};
var clicker = async function() {
var a = await ajax();
console.log('clicker function', a);
};
<div onclick="clicker(event, this);">
Test Me
</div>