Hi All,
I would like to take this opportunity and explain you the asynchronous programming in JavaScript.
Before diving deep into asynchronous concepts like callbacks, promises, async/await.
Javascript is a synchronous single threaded programming language – JavaScript (JS) Engine will execute one task at a time.
JavaScript is also a interpreted programming language – JS Engine executes the code line by line from top to bottom.
So, based on above two sentences we cannot run different task in parallel. Isn’t it? Is it good or bad? If you take other languages like Java or .net we have something called multi threading.
No friends, even in JavaScript we can achieve asynchronous behaviour with the help of callbacks and promises.
callbacks – in simple terms : calling back whenever time comes.
Similarly in JavaScript, if a function “A” gets called by function “B”, somehow we must give the function “A” control to a function “B”.
How to achieve it in JS?
Pass function A as an argument to the function B and function B will take care of function A. That’s all you have to do! 😉
Sample Code Example:
function A(){ console.log(“Function A called!”) } function B(funcA){ console.log(“Inside Function B”) funcA() // function A gets called here! } //calling or invoking function B B(A)
In the above code, we have given complete control of function A to function B.
We will go to next level to understand asynchronous with the help of callbacks. I would strongly say that, in JavaScript asynchronous behaviour exist only because of callbacks!!!
console.log(“Start”) setTimeout(function (){ console.log(“setTimeout Called!”), 5000 } console.log(“End”)
setTimeout is one of the web APIs in JavaScript which takes two arguments 1. callback function and 2. Delay time
When JS engine starts, it goes line by line. So,
First it ogs “Start” in the console.
Then it registers a setTimeout web API in the browser and continues to execute next statements.
Then it logs “End” in the console.
Finally, JS engine waits for the timer (5 sec) to expire then it execute the callback function to log “setTimeout Called!”
This is how we can achieve asynchronous programming in JavaScript.
Continue to this… I will come up with promises and async/await concepts. Stay tuned!
Keep learning and keep sharing 😄
Cheers,
Adarsha 🚀
12 Comments
405 NORAVER P NARANJA MIEL TECNOQUГЌMICAS NORPLANT BAYER SCHERING PHARMA x 12 Pastillas 6 priligy premature ejaculation pills
The effects of ototoxic medication vary from person to person and depend in large part on dosage and the length of time on the medication priligy online
From week 3 to week 12 the test subjects got a weekly injection of 600 mg nandrolone decanoate priligy united states
priligy tablets over the counter How old are you and still love your family Take a break
paxil or priligy Elder abuse Systematic review and implications for practice
Your describe the difference between blood pressure medicines gift to losartan potassium 50 mg para que sirve us last time, Are you still satisfied priligy medicine
The American Academy of Dermatology AAD says that acne affects 50 million Americans every year priligy over the counter Although bladder 2, 2 contains certain nutrients at doses higher than typically recommended to the general public, the nutrients are at or below the tolerable upper intake level Ul
Rosemary oil can be toxic if ingested and should never be taken orally generic priligy online Furthermore, clinical indication in some circumstances may outweigh risks of treatment, and the presence of comorbidities may also influence treatment choice and dosage
If the studies of liver carcinogenicity had been completed and published in the early 1970 s there would be no tamoxifen and tens of thousands of women with breast cancer would have died prematurely priligy amazon uk Infertility has been found to be an important risk factor for ovarian cancer
Assessment of hypoxemia is typically done by SpO2 and assessment of hypercarbia is typically done by ETCO2 priligy near me PMID 18848640 Review
3125 ОјM or higher, however, significant antifungal activity was observed after 6 to 12 h of exposure of C priligy prescription PFS was defined as the time elapsing between the start of tamoxifen treatment for recurrence and the first detection of progressive disease
A total of 181 genes belonging to 13 clusters strongly prognostic HR 3 priligy and viagra combination I also wanted to say that you are not alone