I am fairly new to Javascript and am writing this post off the top of my head, so please correct me if I am wrong with anything.
From what I know, asynchronous functions allow other processes to run in the background and then after the function is completed, it returns a promise that can either be resolved or rejected. Also since processes in async functions don’t run in a set order, we can use the await keyword to stop execution until a process is completed.
However, what baffles me is the difference in code execution between
function f1() {
//do stuff
}
function f2() {
//do stuff
}
function f3() {
//do stuff
}
async function asyncFunc() {
f1()
f2()
f3()
}
asyncFunc()
and this:
async function asyncf1() {
//do stuff
}
async function asyncf2() {
//do stuff
}
async function asyncf3() {
//do stuff
}
function f() {
asyncf1()
asyncf2()
asyncf3()
}
f()
????? (Apart from the obvious fact that the first example returns a promise)
Is calling async functions in a sync function the same thing as calling sync functions in an async function? Do they both result in the same output? If so, which one is preferred over the other?