Thank you guys for your feedback. I upvoted your answers as they both helped me solve this issue, and helped me get to a higher understanding of the matter at hand (but because I'm still new here, the upvote is just like a Promise).
However, I feel like none of them has gotten to the SPECIFIC point that was causing the issue.
Here's the commented working code
// With Callback, Async and Await
async function first(sec){
await new Promise ( (resolve) => setTimeout(()=>{ // this ' (resolve) => ' will make
//sure the Promise is pending until
// the code reaches the
// execution of function resolve()
console.log(1)
resolve(/* return a value */)
/* so, ' (resolve) => ' and then the
' resolve( value ) '
are THE MAIN THING.... "await" will only work if
there is a Promise<pending> right in front of it,
and in order to do that we have to create
a Promise object that will receive a resolve() AND/OR reject() as argument
functions. (or whatever function name we want to use for those parameters).
And when the code fulfills the promise
(by reaching either resolve() or reject()) --- in this case, by reaching
' resolve( value ) ',
the Promise<pending> will turn to Promise<fulfilled> and this
asynchronous part of the code will, then, continue...
*/
},
2000
))
sec()
}
function second(){
console.log(2)
}
console.log("With callback, async, await ...")
first(second)
Now, without callback
// Without Callback, and using .then()
function first(){
return new Promise ( (resolve) => setTimeout(()=>{
console.log(1)
resolve(/* return a value */)
},
2000
))
}
function second(){
console.log(2)
}
console.log("Without callback, and using .then() ...")
first().then(second) // we could also use first().then(()=>second()) if we wanted to
// do something with the returned fulfilled Promise/Response object
// or if we wanted to send the second() function to the
// callback stack before running it