Learning More of Javascript Behavior
I've written an article before that describes my first experience with the asynchronous nature of Javascript. In this article I'm going to expand further on what I've learned
Making The Asynchronous, Synchronous
While going further with the project later did I realize on how to use the sorcery of async and await. Being a lazy uneducated programmer I am. I usually skip explanations in favor the result of the source code or the snippet may bring.
The way I see it, I usually encounter certain types of tutorials or guides; In which I have to watch a whole video, or skim through the whole article just to get an idea on its behavior. This doesn't sit well with me as I'm impatient and lazy. I skip to the part seeing how it works or copy and pasting the code to see if it works then torturing myself on the explanation. Only then ironically, I'll have an interest on almost anything technical.
The simplest and dumbed down explanation on the usage of async / await is to make the process syncrhonous or blocking. As what I've stated in the previous article. I'm used to blocking behavior so I'll assume that the process is synchronous as long as I use the two keywords. And that didn't go well for me at first.
async foo() : Promise {
// process here
return Promise.resolve(true);
}
var bar = await foo();
Yes, I know. It is dumb. I should've been more diligent enough to read first, but in the spirit of programming. Understanding things based on trial and error is where I grew from.
async foo() : Promise {
return new Promise((resolve, reject) => {
// process here
resolve(true);
});
}
var bar = await bar();
It took a good night worth of sleep just to realize that what I've been doing is all wrong. The right way of doing it is to do the process within the Promise
and only then it behaved from what I originally expected.