I have an async function that I wish to wait for until it's done and then make a log as such :
function functionOne(_callback) {
let fetchRes = fetch("https://jsonplaceholder.typicode.com/todos/1");
fetchRes.then(res => res.json()).then(d => {
console.log(d)
})
_callback();
}
function functionTwo() {
// Do some asynchronus work.
functionOne(() => {
console.log("I am a callback, I should come last");
});
}
functionTwo();
However the output is:
"I am a callback, I should come last"
{
completed: false,
id: 1,
title: "delectus aut autem",
userId: 1
}
How can I make console.log
wait for the async function to run in full before running? This code is available in a JSFiddle.